MLStyle.jl
MLStyle.jl copied to clipboard
Speculative idea: 'static' checks for testing if all cases are handled
Most functional languages with pattern matching have a mechanism to check if a pattern matching expression is 'exhaustive'. I'm imagining something like if I wrote
@match x :: Union{Int, Tuple{Int, Int} } begin
(x, y) => x + y
end
throwing an error at compile time, because here we told @match
that x
could be an Int
, or it could be a Tuple
, but our pattern only matches a Tuple
.
Maybe there's a way we could have a pluggable system where users can write 'static completeness checkers' for patterns.
I do agree with the idea to bring exhaustiveness check..
However, this proposal approach can have many problems with respect to its completeness:
function (...)
U = Union{Int, Tuple{Int, Int}}
@match x :: U begin
...
end
end
Should we check?
Besides, if there is a static type parameter T
, what is the expected behaviour of the following one?
@match x :: Union{T, Int} begin
(x, y) => ...
end
If users write x :: U
where U
only contains global references, we can appropriately check the code statically.
why not just write @match
inside a function, it seems simple enough to provide such checks.
This could maybe be a cool use case for JET.jl (Not suggesting adding that as a dependency to MLSyle yet, but maybe I’ll play around in a separate package and try making a @checked_match
or something.