dusklang / dusk

The Dusk Programming Language
Other
0 stars 0 forks source link

Switch expressions and pattern matching #46

Open ghost opened 4 years ago

ghost commented 4 years ago

We should add (at least) the following patterns to the language:

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:

Foo := struct { a: i32 }
val := Option.some(Foo { a: 27 })

// Should warn that all but the case 27 pattern is dead code, but no errors because every possible case is handled:
new_val := switch val {
    Option.some(Foo { a: a @ (case 27 ||| case 35) }) => a / 2
    Option.some(Foo { a }) => a
    Option.none => 2024
}
zachwolfe commented 3 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")