Skip to content

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

@amtoine

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triageAn issue that hasn't had any proper look

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions