Re:Union types ('enum types') would be complicated in Go
by wbl on Saturday December 07, 2024 @12:36AM
(#42347531)
The zero would obviously be the zero value of one of the constituents. Coproducts in most of Go are easy: it's interfaces that make it hard.
Re:Union types ('enum types') would be complicated in Go
by masklinn on Saturday December 07, 2024 @02:48AM
(#42348024)
> The zero would obviously be the zero value of one of the constituents.
If interfaces are used for union types then the obvious zero is nil, not any constituent. Nil is the zero value of interfaces.
It’s perfectly consistent and in line with the rest of the langage.
Re:Union types ('enum types') would be complicated in Go
by wbl on Monday December 09, 2024 @03:53PM
(#42370452)
It's not clear to me that that would satisfy the categorical properties of coproduct that are wanted.
Re:Union types ('enum types') would be complicated in Go
by foldr on Tuesday December 10, 2024 @04:19AM
(#42375189)
Nobody cares about the categorical properties of the coproduct. But in any case, there's no theoretical issue here, since it would be just as if every enum had a 'Nil' variant implicitly defined.
Re:Union types ('enum types') would be complicated in Go
by foldr on Monday December 09, 2024 @11:16AM
(#42367559)
You would want to layer some sugar on top to enable naming of the variants. I was just trying to keep my example as close as possible to valid Go code.
Even if you have to manually name the structs, you'd still have the benefit of exhaustivity checking in type switches, which you can't get with the (perfectly reasonable) approach that you mention.
Honestly interfaces with unexported methods are 90%+ of what people want. It's just not spelled the way they expect. And if you're not going to be happy except at absolutely 100%, a position I can and do respect, there's no point waiting for Go to get any better because I can guarantee you no Go proposal for sum types will fix that you will be forced to have a "nil" value in the sum type, so there's no point in waiting.