Go doesn't have sum types. We emulate them -- most often using the empty
interface{}
in conjunction with a type switch. And that works fine.
Still... I wanted to try out another way of doing this -- and write a code generator for it, which is all the rage. Accidentally, it is the ideal case for a simple code generator, as it requires writing repetitive methods on several types.
The basic idea is to encode a sum type with an interface with one method corresponding to each variant. Those methods are named after the variant type and return both a value of it and a boolean. The return values correspond to a type assertion. For each variant we also add a method that converts it to the sum type.
However, this approach has drawbacks and limitations. We can't define methods on builtin types, types imported from other packages or interfaces, so they cannot be variants if we want all methods to be auto-generated.
And since the actual type is an interface, the language considers nil a permissible value.
First of all, it expects to be run by go generate
. If you do it by hand, set
the value of the GOPACKAGE
environment variable and run it in the right
directory. Having said that, a
//go:generate govariant Sum X Y Z
should generate the code for the type Sum of which X, Y and Z are variants in
the file Sum_variant.go
.
For the following code go generate
will produce a package whose godoc you can
see here.
package example
//go:generate govariant Shape Circle Rectangle
type Cirlce struct {
Center Point
Radius float64
}
type Rectangle struct {
LowerLeft Point
Width, Height float64
}
type Point struct {
X, Y float64
}