2 ms·
The last couple of years I've been doing a lot of exploring different languages and language features. Sum types are one of the few features I've come across t
by gameswithgo 9y ago
The last couple of years I've been doing a lot of exploring different languages and language features. Sum types are one of the few features I've come across that seem to have the potential for large, practical benefit. Not only do you get the potential to eliminate null from a language with Option or Maybe types, but many cases where you might use multiple classes and files to express some hierarchy, can be replaced with just a few lines of code that is so easy to read you can even show it directly to the non programmers working on the project, to work through the definition together.
This especially happens nicely when the syntax is built into the language in a succinct way as in F# / OCaml (possibly others as well!)
- masklinn 9y agoIt's also a convenient way to implement "value-based" state machines (where the entire machine is a single type with a value for each state, as opposed to type-based where each state is implemented as a different — and possibly completely incompatible — static state).
- tomjakubowski 9y ago+1. They are a huge help for managing things like connection state. You can create powerful, statically-enforced invariants, like that a lifetime of a socket is 1:1 with a state of your state machine.