I have been envious of pattern matching for decades. It is one of those language features I saw (almost certainly in OCaml) and just desired it. I have not worked professionally in any language that supports it.
One tiny annoyance I have with many implementations of this feature:
# standalone `in` just returns `false` the pattern doesn't match, useful in `if`:
if point in [x, y]
# it was a 2-element array, and we checked and deconstructed it
# ...
end
This pattern, where `x` and `y` are introduced into the block is a pretty common way to expose a kind of pattern matching guard. You can be sure that inside the `if` block you have valid non-null references to both `x` and `y`.But I hate nesting. In most cases when I am asserting some form of data what I want to do is exit the function if the data is invalid, perhaps with some error code.
So what I want is:
unless point in [x, y]
log.error("Invalid point")
return /* ideally some Option/Result */
end
# code assuming valid x & y
This is one of those "wouldn't it be nice" kinda syntax sugar things, but if I have a series of guards that are progressively narrowing types it ends up feeling much nicer to me.Jakt [1] shares your design philosophy, and has implemented syntax like this: [2]
guard foo is Bar(x) else {
println("not matched")
return -1 as! c_int
}
println("hello there: {}", x)
[1]: https://github.com/SerenityOS/jakt[2]: https://github.com/SerenityOS/jakt/blob/main/samples/guard/i...