Skip to content

sumgen generates interface method implementations from sum-type declarations.

License

Notifications You must be signed in to change notification settings

smasher164/sumgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

sumgen generates interface method implementations from sum-type declarations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages