var foo interface {
struct { A int } | struct { B string }
}
It currently fails with the following error:"cannot use type interface{struct{A int} | struct{B string}} outside a type constraint: interface contains type constraints"
var foo interface {
struct { A int } | struct { B string }
}
It currently fails with the following error:"cannot use type interface{struct{A int} | struct{B string}} outside a type constraint: interface contains type constraints"
(I'm the author of the linked to article.)
In cases where there is a lot of variance in the size of the different interface implementations, separate allocations could actually be more memory efficient than a tagged union. In any case, I'm not sure that memory efficiency is the main reason that people miss Rust-style enums in Go.
I agree with you about the overall motivation for Rust-style enums. I just think it's surprisingly complex to get even the memory efficiency advantages, never mind anything more ambitious.
I'm not sure what you're referring to with 'anything more ambitious'.
The solution to this should be trivial. You just have to extend the gcshape concept to account for the enum discriminator.
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.
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.
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.
var foo interface {
struct { A string } | struct { B string }
}
Eg in Rust that would be 'Result<String, String>', where your success happens to be a String and your errors happens to be a String error message.