effekt
effekt copied to clipboard
[To be discussed] Allow match guards as boolean expressions
When coming from other languages, it is surprising that you can't, e.g. use not(x is Cons(_,Red()))
in an if
/while
.
Also, splitting up longer conditionals using bindings is not possible when using match guards.
Using match guards instead of equality predicates is especially useful on llvm, since there is no infixEq
on data types, and no generic equals
implemented.
Examples
while(not(asBool(cmd is Some("quit")))) {
cmd = readCmd()
//...
}
Proposal
Allow x is p
in expression position and desugar it to if(x is p) { true } else { false }
.
Considerations and Alternatives
- It might be clearer to add some syntax for this, e.g.
asBool(x is p)
(better name required) - Regarding binding, the behaviour is slightly unexpected when changing to booleans. This could be ameliorated by adding a check that the converted pattern does not bind anything. Note that this disallows certain patterns, though (e.g.
(x is Some(y) and valid(y)
). - For the usecase of
while(not...)
, it would be possible to adduntil
, e.g.