Skip to content

list type annotations do not allow default values and specifying generic type #8109

@amtoine

Description

@amtoine

hello there 👋 😋

Describe the bug

Related to the second part of what i'm saying in #7699, the other first part is being addressed in #7699 👌

i'm having trouble making the type annotations of compound types work as i expect

Note
here i take the example of lists, which is a simple compound type

i've got two main issues, as stated in the title of this issue 😋

  • default list values do not allow a function definition to compile
  • i can not give the generic type of list<T> to a signature

How to reproduce

1. the original simple error with default values

define the following function

def foo [my_list: list = ["default value"]] {
  $my_list
}

and get the error

Error: nu::parser::assignment_mismatch (link)

  × Default value wrong type
   ╭─[entry #30:1:1]
 1 │ def foo [my_list: list = ["default value"]] {
   ·                          ────────┬────────
   ·                                  ╰── default value not list<any>
 2 │   $my_list
   ╰────

2. trying to fix it

2.1. no default value

def foo [my_list: list = []] {
  $my_list
}

and

> foo [1, 2, 3]
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯

2.2. complete the annotation

def foo [my_list: list<string> = ["a default string"]] {
  $my_list
}

gives

Error: nu::parser::unknown_type (link)

  × Unknown type.
   ╭─[entry #39:1:1]
 1 │ def foo [my_list: list<string> = ["a default string"]] {
   ·                   ──────┬─────
   ·                         ╰── unknown type
 2 │   $my_list
   ╰────

Expected behavior

1. give default values

def foo [my_list: list = ["a default string"]] {
  $my_list
}

or even

def foo [my_list: list<any> = ["a default string"]] {
  $my_list
}

would give

> foo
╭───┬──────────────────╮
│ 0 │ a default string │
╰───┴──────────────────╯
> foo ["another string"]
╭───┬────────────────╮
│ 0 │ another string │
╰───┴────────────────╯
> foo [1234]
╭───┬──────╮
│ 0 │ 1234 │
╰───┴──────╯

2. generic type annotations

we could even restrict the type with <T>

def foo [my_list: list<string> = ["a default string"]] {
  $my_list
}

would result in

> foo
╭───┬──────────────────╮
│ 0 │ a default string │
╰───┴──────────────────╯
> foo ["another string"]
╭───┬────────────────╮
│ 0 │ another string │
╰───┴────────────────╯

but the following would give an error like

> foo [1234]
Error: nu::parser::type_mismatch (link)

  × Type mismatch.
   ╭─[entry #15:1:1]
 1 │ foo [1234]
   ·     ───┬──
   ·        ╰── expected List(String), found List(Int)
   ╰────

Screenshots

No response

Configuration

key value
version 0.75.1
branch
commit_hash 66398fb
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.66.1 (90743e729 2023-01-10)
rust_channel 1.66.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.66.1 (ad779e08b 2023-01-10)
pkg_version 0.75.1
build_time 2023-02-18 11:04:21 +01:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins gstat

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions