Skip to content

closures and commands don't treat optional parameters the same #11287

Closed

Description

@amtoine

related to

Describe the bug

as per title

How to reproduce

  1. run
let code = {|x?| print ($x | default "i'm the default")}
do $code
  1. see the following error
Error: nu::shell::variable_not_found

  × Variable not found
   ╭─[entry #7:1:1]
 1 │ let code = {|x?| print ($x | default "i'm the default")}
   ·                         ─┬
   ·                          ╰── variable not found
 2 │ do $code
   ╰────
  1. run
def foo [x?] { print ($x | default "i'm the default") }
foo
  1. see the expected output, i.e. i'm the default

Expected behavior

because closures and commands are so similar, i.e. closures are basically unnamed commands, i expected running

let code = {|x?| print ($x | default "i'm the default")}
do $code

to show

i'm the default

Screenshots

No response

Configuration

key value
version 0.87.1
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.73.0 (cc66ad468 2023-10-03)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.73.0 (9c4383fb5 2023-08-26)
build_time 2023-11-21 18:26:35 +01:00
build_rust_channel release
allocator mimalloc
features default, sqlite, trash, which, zip
installed_plugins clipboard copy, clipboard paste, gstat, notify, nu_plugin_explore, query git

Additional context

No response

Activity

  1. added
    status:needs-triage
    on Dec 11, 2023
  2. amtoine commented on Dec 11, 2023

    @amtoine
    MemberAuthor

    could reproduce with nightly

  3. TWSiO commented on Jan 20, 2024

    @TWSiO

    I encountered a problem with the spread operators that is so similar that I feel like the root cause is probably the same (so I decided to comment here rather than create a new issue). If I should create a new issue anyways just in case they don't have the same cause, and maybe just link to this issue, just let me know and I'll make it.

    For closures and commands defined similarly to the above, except using spread operators rather than optional parameters

    let code = {|...x| print ($x | default "i'm the default")}
    def foo [...x] { print ($x | default "i'm the default") }

    Given some parameters they both actually work the same and as expected

    > do $code 1 2 3
    ╭───┬───╮
    │ 0 │ 1 │
    │ 1 │ 2 │
    │ 2 │ 3 │
    ╰───┴───╯
    > foo 1 2 3
    ╭───┬───╮
    │ 0 │ 1 │
    │ 1 │ 2 │
    │ 2 │ 3 │
    ╰───┴───╯

    However, if they are given no arguments, the command works as expected, but the closure gives the same error as above.

    > do $code
    Error: nu::shell::variable_not_found
    
      × Variable not found
       ╭─[entry #53:1:1]
     1 │ let code = {|...x| print ($x | default "i'm the default")}
       ·                           ─┬
       ·                            ╰── variable not found
       ╰────
    > foo
    ╭────────────╮
    │ empty list │
    ╰────────────╯

    Not sure if that will be helpful for fixing the problem, but I thought I may as well add it.

    Also, if it doesn't end up actually being the same cause, just let me know and I'll create a new issue.

  4. added a commit that references this issue on Mar 11, 2024
    5596190
  5. added this to the v0.92.0 milestone on Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions