Giter VIP home page Giter VIP logo

govariant's Introduction

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
}

govariant's People

Contributors

szabba avatar

Watchers

James Cloos avatar Robin avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.