3 ms·
The problem with that is that in Go, I need to be able to put methods on those things, for reasons possibly unrelated to the interface in question. For that the
by jerf 2y ago
The problem with that is that in Go, I need to be able to put methods on those things, for reasons possibly unrelated to the interface in question. For that they need to be named. For that you might as well do what has worked since Go 1.0 and just put an unexported method in the interface and declare several instances of that interface in your package.
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.
- wbl 2y agoThe 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.
- masklinn 2y ago> 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.
- wbl 2y agoIt's not clear to me that that would satisfy the categorical properties of coproduct that are wanted.
- foldr 2y agoNobody 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.
- foldr 2y agoYou might 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 define separate named structs, you still have the benefit of exhaustivity checking in type switches. That's arguably the other 10% that people want.