Re:Golang: How to Update APIs for Generics
by cube2222 on Thursday September 16, 2021 @10:40AM
(#28552415)
Approximating closed sum types is easy already, by creating an interface with a stub private method and implementing it for all variants. Then it's not possible to implement it using a type outside of that package.
The main thing that's lacking is compile time checks whether variant matching is exhaustive.
You can define an interface with a closed set of types
type Number interface { float64, int64, ... }
Now, the value will be boxed, so you will not have no-allocation sum types but you can approximate boxed sum types with this I think.