Skip to content

Latest commit

 

History

History
52 lines (40 loc) · 1.15 KB

README.md

File metadata and controls

52 lines (40 loc) · 1.15 KB

sumgen generates interface method implementations from sum-type declarations.

go get -u github.com/smasher164/sumgen

Usage: sumgen is a tool intended for go generate, but can also be run standalone. An example declaration of a sum-type would be

package main

//go:generate sumgen "Expr = KeyValueExpr | *CallExpr"
type Expr interface {
	expr()
}

type KeyValueExpr struct {
	Key   Expr
	Colon int
	Value Expr
}
type CallExpr struct {
	Fun      Expr
	Lparen   int
	Args     []Expr
	Ellipsis bool
	Rparen   int
}

func main() {}

Running go generate in the package directory will output the following to a file named DIRECTORY_NAME_sumgen.go.

// Code generated by "sumgen"; DO NOT EDIT.

package main

func (_ KeyValueExpr) expr() { panic("default implementation") }
func (_ *CallExpr) expr()    { panic("default implementation") }

Each declaration follows the following structure:

Def = LhsType "=" RhsType { "|" RhsType } .
LhsType = identifier .
RhsType = [ * ] identifier .

More than one declaration can be be made per interface_name. To make the concrete_typename a pointer receiver, simply add a '*' in front of it.