4 ms·
> Why not? Generally the zero-value for a union type is just the first option, which can be useful or not. You can wrap it in an option type to ensure it's been
by ncruces 2y ago
> Why not? Generally the zero-value for a union type is just the first option, which can be useful or not. You can wrap it in an option type to ensure it's been initialized too.
Races with the GC, if you try to be smart and have unboxed options.
Interfaces are similarly boxed to avoid such races, and make the rest of the language faster.
> If Go's semantics mean it's always a nullable pointer, then that still doesn't make union types useless. There's still value in knowing that "if there's a value, it's one of these Y options".
At that point, what's the benefit over a (boxed) interface and existing type assertions/switch?
Besides, if the options are your own types, you can also add an unexported method to the interface to seal it. And then, if you go this route, you can also have exhaustiveness checks with existing tooling.