3 ms·
> It seems to me the next object of hatred is the lack of sum types. Sum types may be coming in a close future release, since the notation type T interface
by ainar-g 5y ago
> It seems to me the next object of hatred is the lack of sum types.
Sum types may be coming in a close future release, since the notation
type T interface {
int | string
}
that can currently only be used for constraints on type parameters can be “easily” made to be a runtime thing as well. I cannot find the GitHub issue right now (perhaps, it wasn't an issue, but a comment in a very long thread, and GitHub is being annoying with those), but it seems like nobody is currently opposed to that, and the only reason it didn't make it into 1.18 is that it's already a pretty big language change.
- earthboundkid 5y agoYes, the only downside I see is that if you do: type Number interface { int | int64 | int32 } var n Number The default value for n has to be nil, rather than 0, because Go can't choose between the types (int 0, int64 0, etc.). And of course, you can't do n + x (assuming x is also a Number) because x might be a different kind of number. Still, it's a pretty good simplification from the current restrictions, and it builds well on existing concepts.