Skip to content

generate: switch the position of <initial> and <closure>, so the closure can have default parameters - #13393

Merged
devyn merged 12 commits into
nushell:mainfrom
WindSoilder:generate_arg
Jul 19, 2024
Merged

devyn merged 12 commits into
nushell:mainfrom
WindSoilder:generate_arg

Conversation

@WindSoilder

@WindSoilder WindSoilder commented Jul 17, 2024 •

Copy link
Copy Markdown
Contributor

Description

Close: #12083
Close: #12084

User-Facing Changes

It's a breaking change because we have switched the position of <initial> and <closure>, after the change, initial value will be optional. So it's possible to do something like this:

> let f = {|fib = [0, 1]| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} }
> generate $f | first 5
╭───┬───╮
│ 0 │ 0 │
│ 1 │ 1 │
│ 2 │ 1 │
│ 3 │ 2 │
│ 4 │ 3 │
╰───┴───╯

It will also raise error if user don't give initial value, and the closure don't have default parameter.

❯ let f = {|fib| {out: $fib.0, next: [$fib.1, ($fib.0 + $fib.1)]} }
❯ generate $f
Error:   × The initial value is missing
   ╭─[entry #5:1:1]
 1 │ generate $f
   · ────┬───
   ·     ╰── Missing intial value
   ╰────
  help: Provide <initial> value in generate, or assigning default value to closure parameter

Tests + Formatting

Added some test cases.

@WindSoilder WindSoilder added notes:breaking-changes Noted in Breaking Changes section deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way labels Jul 17, 2024
Co-authored-by: Stefan Holderbach <[email protected]>
@devyn

devyn commented Jul 19, 2024

Copy link
Copy Markdown
Contributor

Sure, this sounds like an easy enough thing that typechecking will be able to help with

@devyn
devyn merged commit e281c03 into nushell:main Jul 19, 2024
@hustcer hustcer added this to the v0.96.0 milestone Jul 19, 2024
@WindSoilder
WindSoilder deleted the generate_arg branch October 14, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow generate to accept just a closure with a default value Swap the argument order for generate to be consistent with do

4 participants