3 ms·
Unlike subtyping, sum types are bounded - you can't add new variants without modifying the originally-defined type. That's a huge difference in terms of code a
by yen223 9y ago
Unlike subtyping, sum types are bounded - you can't add new variants without modifying the originally-defined type.
That's a huge difference in terms of code architecture - it enables things like exhaustive pattern matching, and allows the programmer to enforce stronger invariants in the code.
- rbehrends 9y agoSubtyping can be closed, too (see Scala's sealed classes, for example) and algebraic data types can be open (see OCaml's extensible variants [1] or polymorphic variants [2]). This is simply a distinction between closed and open sum types. [1] https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s%3Aextensible-variants https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s%3Aex... [2] http://caml.inria.fr/pub/docs/manual-ocaml-400/manual006.html#toc36 http://caml.inria.fr/pub/docs/manual-ocaml-400/manual006.htm...