Open ghost opened 4 years ago
Idea for destructuring: take a hybrid of Rust and Jai syntax. The new variable declaration syntax would consist of irrefutable-pattern : type? = value
. Instead of :=
for variables and ::
for constants, you'd have constname := value
for constants and mut varname := value
for variables. Then destructuring falls out: [a, mut b] := "string".split("r")
We should add (at least) the following patterns to the language:
case expr
mut
prefix to make the variable mutable. For example:foo
,mut bar
StructName { field_a: pattern, field_b: pattern }
StructName { field_a: pattern, .. }
when you only care about some fieldsStructName { field_a, .. }
when you want a binding pattern with the same name as the fieldEnumName.variant
,EnumName.tuple_variant(pattern, pattern)
orEnumName.struct_variant { field_a: pattern, field_b: pattern }
depending on the kind of variant (same shorthands apply to struct variant patterns as struct patterns)[pattern, pattern]
pattern2 @ pattern2
pattern1 ||| pattern 2
To make use of these patterns, we should add a
switch
expression. It needs to do exhaustiveness checking at compile time, which would benefit from using the refinement checker, I think. In addition to exhaustiveness, if there are two or more equivalent patterns, we should emit an error.Code sample: