do command: Make closure support default parameters and type checking#12056
do command: Make closure support default parameters and type checking#12056WindSoilder merged 6 commits intonushell:mainfrom
do command: Make closure support default parameters and type checking#12056Conversation
|
While I can kind of follow what you are doing for Can we perform type checking? And do we have any remaining commands that sniff for the number of parameters of a closure to decide what they do? Here default values may complicate the semantics. |
I don't think we need to handle for them, because many other commands like
I'm not really sure if it's worth to add type checking for something like |
|
I am OK with going step by step and landing this improvement for |
do command: Make closure support default parameters and type checking
|
Sure! I have changed the title and description |
and |
|
Also, FWIW, However, it's probably worth considering allowing [ 1 2 3 4 5 ] | reduce {|it,acc=10| $acc + $it }This would be expected to return As far as I can tell, |
# Description Fixes: #12690 The issue is happened after #12056 is merged. It will raise error if user doesn't supply required parameter when run closure with do. And parser adds a `$it` parameter when parsing closure or block expression. I believe the previous behavior is because we allow such syntax on previous version(0.44): ```nushell let x = { print $it } ``` But it's no longer allowed after 0.60. So I think they can be removed. # User-Facing Changes ```nushell let tmp = { let it = 42 print $it } do -c $tmp ``` should be possible again. # Tests + Formatting Added 1 test
# Description Fixes: nushell#12690 The issue is happened after nushell#12056 is merged. It will raise error if user doesn't supply required parameter when run closure with do. And parser adds a `$it` parameter when parsing closure or block expression. I believe the previous behavior is because we allow such syntax on previous version(0.44): ```nushell let x = { print $it } ``` But it's no longer allowed after 0.60. So I think they can be removed. # User-Facing Changes ```nushell let tmp = { let it = 42 print $it } do -c $tmp ``` should be possible again. # Tests + Formatting Added 1 test
# Description #12056 added support for default and type-checked arguments in `do` closures. This PR adds examples for those features. It also: * Fixes the TODO (a closure parameter that wasn't being used) that was preventing a result from being added * Removes extraneous commas from the descriptions * Adds an example demonstrating multiple positional closure arguments # User-Facing Changes Help examples only # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
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.nushell/crates/nu-engine/src/eval.rs
Lines 60 to 130 in 3ee2fc6
It only works for
docommand.User-Facing Changes
Closure supports optional parameter
Previously it raises an error, after this change, it prints
i'm the default.Closure supports type checking
After this change, it will raise an error with a message:
can't convert string to intTests + Formatting
Done
After Submitting
NaN