forked from extern/nushell
Finish adding support for optional params (#530)
This commit is contained in:
parent
e949658381
commit
caf73c36f2
@ -39,18 +39,24 @@ fn eval_call(
|
|||||||
let block = engine_state.get_block(block_id);
|
let block = engine_state.get_block(block_id);
|
||||||
|
|
||||||
let mut stack = stack.collect_captures(&block.captures);
|
let mut stack = stack.collect_captures(&block.captures);
|
||||||
for (arg, param) in call.positional.iter().zip(
|
|
||||||
decl.signature()
|
for (param_idx, param) in decl
|
||||||
.required_positional
|
.signature()
|
||||||
.iter()
|
.required_positional
|
||||||
.chain(decl.signature().optional_positional.iter()),
|
.iter()
|
||||||
) {
|
.chain(decl.signature().optional_positional.iter())
|
||||||
let result = eval_expression(engine_state, &mut stack, arg)?;
|
.enumerate()
|
||||||
|
{
|
||||||
let var_id = param
|
let var_id = param
|
||||||
.var_id
|
.var_id
|
||||||
.expect("internal error: all custom parameters must have var_ids");
|
.expect("internal error: all custom parameters must have var_ids");
|
||||||
|
|
||||||
stack.add_var(var_id, result);
|
if let Some(arg) = call.positional.get(param_idx) {
|
||||||
|
let result = eval_expression(engine_state, &mut stack, arg)?;
|
||||||
|
stack.add_var(var_id, result);
|
||||||
|
} else {
|
||||||
|
stack.add_var(var_id, Value::nothing(call.head));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(rest_positional) = decl.signature().rest_positional {
|
if let Some(rest_positional) = decl.signature().rest_positional {
|
||||||
|
@ -1300,3 +1300,11 @@ fn get_table_columns_1() -> TestResult {
|
|||||||
fn get_table_columns_2() -> TestResult {
|
fn get_table_columns_2() -> TestResult {
|
||||||
run_test("[[name, age, grade]; [paul,21,a]] | columns | nth 1", "age")
|
run_test("[[name, age, grade]; [paul,21,a]] | columns | nth 1", "age")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn allow_missing_optional_params() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
"def foo [x?:int] { if $x != $nothing { $x + 10 } else { 5 } }; foo",
|
||||||
|
"5",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user