1 min readApr 9, 2017
Interestingly, from your description of a binary tree, it could also be defined in Haskell as:
data Tree = Node (Maybe Tree) Int (Maybe Tree)
Then a leaf node would look like:
node = Node Nothing 1 Nothing
For completeness, we should also mention that in OOP the second-closest thing to sum types is an inheritance hierarchy of a base type and derived types. The base type models the overall type and the derived types model the ‘either … or’ data cases. E.g. the above tree type and value in Java:
interface Tree {}
class Node extends Tree {
private Tree _left; private int _value; private Tree _right; public Node(
Optional<Tree> left, int value, Optional<Tree> right) {
_left = left; _value = value; _right = right;
} public Tree getLeft() { return _left; }
public int getValue() { return _value; }
public Tree getRight() { return _right; }
}Tree node = new Node(Optional.empty(), 1, Optional.empty());
This syntactic difference really emphasises how much thought has gone into making sum types usable in ML-derived languages.