sumgen

command module
v0.0.0-...-63acac9 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2019 License: MIT Imports: 17 Imported by: 0

README

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.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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