3 ms·
Product types + nullability is the same as sum types if you treat it that way, but it's less nice because the data representation in memory will not be as compa
by dilap 5y ago
Product types + nullability is the same as sum types if you treat it that way, but it's less nice because the data representation in memory will not be as compact, and you won't get any guarantees from your language that you didn't fuck up (set less or more than exactly one field). Plus also you have to use comments to indicate your intention.
Relatedly, a single field of an interface type + the ability to cast to a concrete type is very similar to a sum type; Go programmers will use this trick sometimes.
Sum type:
I can be A | B | C.
Interface thype:
I am ILetter. Types A, B, C implement ILetter.
Plausible in memory representation of sum type value: 8 bits for the type tag, plus max-size(A,B,C) bits for type.
Plausible in memory representation of interface type value: 64 bits for the type pointer and 64 bits for a pointer to A, B or C.
(Not as nice as the sum type!)