-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow default values for closure parameters #11318
Copy link
Copy link
Closed
Labels
A:syntaxChanges to the grammar or syntax beyond parser bugfixesChanges to the grammar or syntax beyond parser bugfixescategory:enhancementNew feature or requestNew feature or requeststatus:needs-triageAn issue that hasn't had any proper lookAn issue that hasn't had any proper look
Milestone
Description
Related problem
When defining a custom command, we can obviously specify default parameters, e.g.:
def greet [ name="World", greeting="Hello" ] {
$"($greeting), ($name)"
}
> greet
Hello, World
> greet Dave
Hello, Dave... etc.
However, this doesn't work for the equivalent closure:
let greet = {
|name="World", greeting="Hello"|
$"($greeting), ($name)"
}
> do $greet
Error: nu::shell::variable_not_found
× Variable not found
╭─[entry #5:1:1]
1 │ let greet = { |name="World", greeting="Hello"| $"($greeting), ($name)"}
· ────┬────
· ╰── variable not found
╰────
> do $greet Dave Bonjour
Bonjour, DaveDescribe the solution you'd like
Allow do $greet to work with the default parameter values.
Describe alternatives you've considered
No response
Additional context and details
My ultimate goal is using these with generate in a more syntactically pleasing manner. E.g., using a modified example from the help:
> let fibGenerator = {
|fib = [ 0,1 ]|
{
out: $fib.0,
next: [$fib.1, ($fib.0 + $fib.1)]
}
}
> generate $fibGenerator | first 5
╭───┬───╮
│ 0 │ 0 │
│ 1 │ 1 │
│ 2 │ 1 │
│ 3 │ 2 │
│ 4 │ 3 │
╰───┴───╯It seems to me that most generators are going to have a default value anyway (e.g., page = 1 for a paging API call). It seems more readable to have it as part of the generator definition than the invocation. E.g., the way it is today:
generate [ 0, 1 ] $fibGenerator | first 5Of course, having this work with generators will require additional work, but allowing closure parameters to use default values (this issue) is a pre-req for that.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A:syntaxChanges to the grammar or syntax beyond parser bugfixesChanges to the grammar or syntax beyond parser bugfixescategory:enhancementNew feature or requestNew feature or requeststatus:needs-triageAn issue that hasn't had any proper lookAn issue that hasn't had any proper look