allow lists to have type annotations#8529
allow lists to have type annotations#8529sholderbach merged 5 commits intonushell:mainfrom 1Kinoti:annotations
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #8529 +/- ##
==========================================
+ Coverage 68.44% 68.49% +0.04%
==========================================
Files 628 628
Lines 101551 101631 +80
==========================================
+ Hits 69508 69610 +102
+ Misses 32043 32021 -22
|
sholderbach
left a comment
There was a problem hiding this comment.
Nice! Looks like you figured a relatively straightforward patch. And great to see all the tests!
|
very nice :ok_hand |
|
Will this feature be ported to |
|
yes of course, after lists have been stabilized, then records, tables, and closures can be considered |
sholderbach
left a comment
There was a problem hiding this comment.
I checked that the lexing changes don't conflict with the new match syntax. So let's merge this as the changes look good and well covered.
Thanks for tackling that @1Kinoti
|
thanks! is it okay if i now tackle records and tables? |
I'm anxious to see what that looks like and I'm excited to try this out more. Good work @1Kinoti! |
|
@1Kinoti Here's something I stumbled upon while testing this. Given this custom example: I expected the last test to produce and error because I defined |
|
@fdncred especially since def foo [x: list<int> = [1 2 3]] { print $x }works as expected. |
|
but really love the feature 🤩 |
|
good catch @fdncred , let me look into it |
|
i have opened #8600 that addresses a semi-related issue, but this def foo [my_list: list<string> = ['val1' 'val2']] {
$my_list
}
foo [1 2 3] # okay but shouldn't beis still not flagged as an error because the parser now parsed the (foo [1 2 3) | describe # list<string>good news is that it only happens with strings, however, bad news also |
# Description @fdncred noticed an [issue](#8529 (comment)) with list annotations that while i was trying to find a fix found another issue. innitially, this was accepted by the parser ```nu def err [list: list<int> = ['a' 'b' 'c']] {} ``` but now an error is raised ```nu Error: nu::parser::assignment_mismatch × Default value wrong type ╭─[entry #1:1:1] 1 │ def err [list: list<int> = ['a' 'b' 'c']] {} · ──────┬──── · ╰── expected default value to be `list<int>` ╰──── ``` # User-Facing Changes none # Tests + Formatting done
|
this issue if still present with something like def foo [bar: string] { $bar }foo 69 # is okay, but shouldn't be
foo 69 | describe # string |
# Description follow up to #8529 cleaned up version of #8892 - the original syntax is okay ```nu def okay [rec: record] {} ``` - you can now add type annotations for fields if you know them before hand ```nu def okay [rec: record<name: string>] {} ``` - you can specify multiple fields ```nu def okay [person: record<name: string age: int>] {} # an optional comma is allowed def okay [person: record<name: string, age: int>] {} ``` - if annotations are specified, any use of the command will be type checked against the specified type ```nu def unwrap [result: record<ok: bool, value: any>] {} unwrap {ok: 2, value: "value"} # errors with Error: nu::parser::type_mismatch × Type mismatch. ╭─[entry #4:1:1] 1 │ unwrap {ok: 2, value: "value"} · ───────┬───── · ╰── expected record<ok: bool, value: any>, found record<ok: int, value: string> ╰──── ``` > here the error is in the `ok` field, since `any` is coerced into any type > as a result `unwrap {ok: true, value: "value"}` is okay - the key must be a string, either quoted or unquoted ```nu def err [rec: record<{}: list>] {} # errors with Error: × `record` type annotations key not string ╭─[entry #7:1:1] 1 │ def unwrap [result: record<{}: bool, value: any>] {} · ─┬ · ╰── must be a string ╰──── ``` - a key doesn't have to have a type in which case it is assumed to be `any` ```nu def okay [person: record<name age>] {} def okay [person: record<name: string age>] {} ``` - however, if you put a colon, you have to specify a type ```nu def err [person: record<name: >] {} # errors with Error: nu::parser::parse_mismatch × Parse mismatch during operation. ╭─[entry #12:1:1] 1 │ def unwrap [res: record<name: >] { $res } · ┬ · ╰── expected type after colon ╰──── ``` # User-Facing Changes **[BREAKING CHANGES]** - this change adds a field to `SyntaxShape::Record` so any plugins that used it will have to update and include the field. though if you are unsure of the type the record expects, `SyntaxShape::Record(vec![])` will suffice
# Description follow up to #8529 and #8914 this works very similarly to record annotations, only difference being that ```sh table<name: string> ^^^^ ^^^^^^ | | | represents the type of the items in that column | represents the column name ``` more info on the syntax can be found [here](#8914 (comment)) # User-Facing Changes **[BREAKING CHANGE]** this change adds a field to `SyntaxShape::Table` so any plugins that used it will have to update and include the field. though if you are unsure of the type the table expects, `SyntaxShape::Table(vec![])` will suffice
this pr refines #8270 and closes #8109
description
examples:
the original syntax is okay
empty annotations are allowed in any variation
the last two may be caught by a future formatter,
but do not affect
nucode currentlytypes are allowed (See notes below)
nested annotations are allowed in many variations
any unterminated annotation is caught
unknown types are flagged
notes
the error message for mismatched types in not as intuitive
it should be something like this
this is currently not implemented