| ▲ | 9rx 4 hours ago | ||||||||||||||||
Using interface as it was designed to be used offers first-class sum types. Although not all interface use equates to sum types. But they're not tagged unions. I expect that is still where your confusion lies. Tagged unions and sum types are not equivalent. Tagged unions are a subset of sum types. | |||||||||||||||||
| ▲ | josephg an hour ago | parent | next [-] | ||||||||||||||||
This may be a bit too pedantic, but I consider interface {} to be a way to do polymorphism via type classes. Interface defines an open class of types which implement some interface. Sum types are a type definition defining something as A or B. Not “anything that quacks like a duck”. But concretely “one of this or one of that”. This enables different syntax, like the match expression to be used, in which you exhaustively list all the variants. The compiler doesn’t need to heap allocate enums because it knows the maximum size of a single value. The compiler and programmer can take advantage of the knowledge that there’s a closed set of values the type can hold. It’s not an open type class. Result and Option are quite beautiful as sum types. But they’re horrible as type classes. Try implementing them using interface{} in Go. It’s not the same. | |||||||||||||||||
| |||||||||||||||||
| ▲ | lock1 3 hours ago | parent | prev [-] | ||||||||||||||||
I'm curious, if tagged unions are a subset of sum type, what is your definition of "sum type"? AFAIK, tagged union is sum type, based on sum type mathematical definition. | |||||||||||||||||
| |||||||||||||||||