by dmitriid · 6y ago · 0 comments
One of the greatest powers of pattern matching comes from being abole to match in function definitions. The simplest example:
fac(0) -> 1;
fac(N) when N > 0 -> N*fac(N-1).
This makes a lot of code much more readable and more concise.[1] https://github.com/tc39/proposal-pattern-matching
[2] http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.h...
Much like how the compiler assists you when writing subclasseses so that you've implemented all the required methods, real Pattern Matching (F#) gives you the same guarantees from the compiler that you've handled all the possible cases appropriately.
Exhaustiveness checking is what elevates pattern matching to the other side of the expression problem, which is about getting feedback from the compiler to help you write programs.
a glorified switch statement does not help you do that, and so you still have to write subclasses to get compiler feedback.
It's 'fixed' in the same sense as the compiler knowing what are the subclasses of a base class.
Just as you can always 'extend' the OOP base class by adding a subclass, you can always 'extend' an algebraic data for by adding another case.
Both can be extended, but more importantly they can be enumerated.
... but the compiler does not know ? new classes are loaded at run-time all the time with e.g. plug-in systems, etc.
Without exhaustiveness it might not be feasible for a statically types PL though, but I don't know nearly enough to even speculate on that.
They don't in scala either and I had assumed that was to do with type erasure. Given F# does however, presumably there is no limitation to C# doing it?
Because then you would need to limit matching to some kind of closed algebraic data type (ADT) so that compiler knows what is left to be matched on, or provide a catch-all clause.
I thought scala does produce partial warnings matching on case classes