6 ms·
> Pattern matching without support for algebraic datatypes really misses the point. That's really not true, Erlang has neither static types nor sum types (whic
by masklinn 9y ago
> Pattern matching without support for algebraic datatypes really misses the point.
That's really not true, Erlang has neither static types nor sum types (which I expect is what you mean by ADT rather than including product types) yet several of its construct (not least of which "assignment") make great use of pattern matching. Pattern matching is one of the most important tools in Erlang's toolbox.
> What you want is to be able to declare all the shapes of your data, with descriptive names, and then be able to say definitely "these are the only such shapes."
> Pattern matching without exhaustiveness checking and algebraic datatypes is really just a disguise
Also not necessarily true, closed sum types are probably the best default but OCaml's polymorphic and extensible variants are genuinely great tools.
- platz 9y ago'the point' that OP cares about is exhaustiveness. one can disagree of course; but without exhaustiveness what you really have is destructuring (this is not what OP cares about)- erlang is a fine example
- masklinn 9y ago> without exhaustiveness what you really have is destructuring- erlang is a fine example That's completely nonsensical. Erlang does not have exhaustiveness yet is not limited to destructuring. In fact, mere destructuring is probably the least common of pattern matching's uses in Erlang, much more common are asserting, filtering and dispatching (all of which may make further use of destructuring internally but destructuring is a sub-component of the match rather than the pricipal).
- tarsinge 9y agoNot just destructuring, see https://elixir-lang.org/getting-started/pattern-matching.html https://elixir-lang.org/getting-started/pattern-matching.htm... for example
- rbehrends 9y agoWithout exhaustiveness checking, you still get a more advanced typecase-like control flow construct, not just destructuring. In practice (and non-trivial code), exhaustiveness checking plays much less of a role, especially if you already have an object-oriented language. In basic OO languages, you get exhaustiveness through abstract methods AND get the ability to add subtypes to an existing sum type. One advantage of pattern matching is that it's easier to add actions (instead of subtypes), a.k.a. the expression problem. But that's a modularity issue, has nothing to do with exhaustiveness, and can also go the other way (as pattern matching makes it hard to add new variants to an ADT in a modular fashion). Another advantage is that pattern matching can be a poor person's tree parser, but for that, exhaustiveness doesn't buy you much, because ADT patterns also typically allow for patterns that aren't allowed by the underlying tree grammar, so you'll have an `_ -> assert false` fallback case often enough.
- still_grokking 9y ago> But that's a modularity issue, has nothing to do with exhaustiveness, and can also go the other way (as pattern matching makes it hard to add new variants to an ADT in a modular fashion). I would disagree on this point. With exhaustiveness checks you get at least errors in every place you match against a data type you just extended with a new variant. So your compiler can point you to the places you have to edit after adding new variants to your ADT. That's a helpful feature.
- rbehrends 9y agoNote that I talked about doing it in a modular fashion. With an ADT, you have to rewrite every single pattern match in your code to support the new variant. (And you don't even get warnings if you have a general fallback case that matches everything.) In an OO language, the equivalent to simple pattern matching would be to dispatch on an abstract method of a shared supertype. If you add a new type and forget to implement the abstract method, you get a compiler error. There's your exhaustiveness checking and it's entirely modular as you don't have to touch your original code. As I said, that's one side of the expression problem. OO languages conversely have a problem (unless they support multiple dispatch or methods external to a class) when they have to add new actions. With ADTs, that's just a new function. With inheritance, you have to add a new method to every single class that implements a variant.
- hyyypr 9y agoThe implementation in the blog post does handle exhaustiveness via the "default" case of the switch statement.
- yen223 9y agoWhat we want from exhaustive pattern checks is the ability to check on all cases without falling back on the "default" case. Having the compiler warn you about missing checks is a powerful thing to have. This is easy to achieve if you're matching on a variant type, since there's only a finite number of them.