Skip to content

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

Closed

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

Activity

  1. fdncred commented on Feb 18, 2023

    @fdncred
    Contributor

    Have you tried out this yet to see if it helps? #8096

  2. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    Have you tried out this yet to see if it helps? #8096

    nope, but i sure can 👍

  3. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    i think it works 😮
    at least for the first part of this issue 👍

    we still can not narrow down the types of the arguments with list<any> or list` for instance.

  4. fdncred commented on Feb 18, 2023

    @fdncred
    Contributor

    I looked at adding list<string> etc, but couldn't figure it out. It'll take someone smarter with the parser than me.

  5. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    I looked at adding list<string> etc, but couldn't figure it out. It'll take someone smarter with the parser than me.

    no worries 👍

    then we might want to keep that open for future tracking

  6. amtoine commented on Feb 22, 2023

    @amtoine
    MemberAuthor

    the first part of this issue has been fixed by #8096 👌

  7. linked a pull request that will close this issueallow lists to have type annotations #8270on Mar 5, 2023
  8. added a commit that references this issue on Mar 24, 2023
    8cf9bc9
  9. added this to the v0.78.0 milestone on Mar 26, 2023
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions