2 ms·
>there is no way to manufacture a zero value of Username out of thin air As I said, with the proposal above, you cannot manufacture a default zero value from a
by kgeist 2y ago
>there is no way to manufacture a zero value of Username out of thin air
As I said, with the proposal above, you cannot manufacture a default zero value from a union if you specify in the language's spec that trying to retrieve a value from a non-initialized ("not active") subtype panics (i.e. the union was constructed with a different underlying type)
asUsername := myUnion.(UserName) // panics if myUnion doesn't hold UserName internally
// if you want to retrieve the value, you type-switch:
switch myUnion.(type) {
case UserName:
// if the underlying type is UserName, it always returns the value as it was passed into the union's constructor by the developer, it doesn't matter if it was "initialized" or the developer passed some default zero'd value; it's not the union's concern at all
}
It's similar to panicking on nil interfaces, nil maps etc. The article is called "Good union types in Go would probably need types without a zero value", so I responded to that -- you don't need types without a zero value for the use case of union types. It's also different from interfaces (as you noted earlier), because this allows disjoint types.
Whether Go must allow non-zero types in general is a totally different topic.