3 ms·
Unless I'm missing something, enums are closed, thus overlap with sum types for the use-cases. Subclasses are an implementation detail in this context. Here is
by pacala 12y ago
Unless I'm missing something, enums are closed, thus overlap with sum types for the use-cases. Subclasses are an implementation detail in this context. Here is an example that uses subclasses to implement generalized enums, aka sum types:
enum List {
Cons(head, tail) {...}
Nil {...}
int length() {
switch (this) {
case Cons(head, tail): return 1 + length(tail)
case Nil: return 0
}
}
}
Mind you, this is syntactic sugar and you can do it by hand with enums + sublcasses. But the whole point of clean languages is to remove unnecessary boilerplate :)
- jdonaldson 12y agoHaxe has another example of this: http://haxe.org/manual/types-enum-instance.html http://haxe.org/manual/types-enum-instance.html Note that enums in Haxe are pure data types, and can't have member functions and/or values. However, since enums are public by nature, it's possible to achieve this easily with "using": http://try.haxe.org/#87bF5 http://try.haxe.org/#87bF5