Adding Type Annotations in patterns #3103
jrstrunk
started this conversation in
Ideas & suggestions
Replies: 1 comment 2 replies
-
Hello!
This is valid code today. The first example with the tuple annotation is written like so: use #(length, rest): #(BitArray, _) <- result.then(consume_till_crlf(rest, <<>>)) An aside: In Gleam it is called pattern matching rather than destructuring. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What does anyone think about adding the ability to add type annotations when pattern matching? An example of how this can be used straight from the radish package's decoder.gleam file is:
This is currently a compile error since I cannot tell the compiler that I am expecting the
length
variable from theconsume_till_crlf
function to be a BitArray. This might be nice because currently if in my code I am expecting a BitArray value for the first element, but theconsume_till_crlf
function returns something else, say aString
, then I will get an error when I callbit_array.to_string
, even though that may be correct in the context of my application and it is theconsume_till_crlf
function that is returning the wrong type.In the current version of Gleam, the above code snippet actually looks like this:
*but could look like this:
Which in my opinion is a little less easy to read because it is not obvious at a glance why length is redeclared three times.
So, this addition could add:
Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions