govariant

command module
v0.0.0-...-4bfa621 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2015 License: MPL-2.0 Imports: 8 Imported by: 0

README

# 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
//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](http://godoc.org/github.com/szabba/govariant/example).

```go
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
}
```

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL