Skip to content
This repository has been archived by the owner on Nov 12, 2017. It is now read-only.

szabba/govariant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A generator for Go(lang) sum types

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.

How to use it

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.

Example

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
}

About

[recommend using https://github.com/szabba/irgen instead] Sum type generator for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages