do command: Make closure support default parameters and type checking (#12056)

# Description
Fixes: #11287
Fixes: #11318

It's implemented by porting the similar logic in `eval_call`, I've tried
to reduce duplicate code, but it seems that it's hard without using
macros.

3ee2fc60f9/crates/nu-engine/src/eval.rs (L60-L130)

It only works for `do` command.

# User-Facing Changes
## Closure supports optional parameter
```nushell
let code = {|x?| print ($x | default "i'm the default")}
do $code
```
Previously it raises an error, after this change, it prints `i'm the
default`.

## Closure supports type checking
```nushell
let code = {|x: int| echo $x}
do $code "aa"
```
After this change, it will raise an error with a message: `can't convert
string to int`

# Tests + Formatting
Done

# After Submitting
NaN
This commit is contained in:
Wind
2024-03-11 18:11:08 +08:00
committed by GitHub
parent 27edef4874
commit 5596190377
2 changed files with 95 additions and 38 deletions

View File

@@ -521,6 +521,26 @@ fn run_dynamic_closures() {
assert_eq!(actual.out, "holaaaa");
}
#[test]
fn dynamic_closure_type_check() {
let actual = nu!(r#"let closure = {|x: int| echo $x}; do $closure "aa""#);
assert!(actual.err.contains("can't convert string to int"))
}
#[test]
fn dynamic_closure_optional_arg() {
let actual = nu!(r#"let closure = {|x: int = 3| echo $x}; do $closure"#);
assert_eq!(actual.out, "3");
let actual = nu!(r#"let closure = {|x: int = 3| echo $x}; do $closure 10"#);
assert_eq!(actual.out, "10");
}
#[test]
fn dynamic_closure_rest_args() {
let actual = nu!(r#"let closure = {|...args| $args | str join ""}; do $closure 1 2 3"#);
assert_eq!(actual.out, "123");
}
#[cfg(feature = "which-support")]
#[test]
fn argument_subexpression_reports_errors() {