Skip to content

This Go module provides a boilerplate generator for modelling compiler checked tagged union / sum types / choice types

License

Notifications You must be signed in to change notification settings

worldiety/choicegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

choicegen

This Go module provides a boilerplate generator for modelling compiler checked tagged union / sum types / choice types.

In a limited way, this can be used like the proposed sum types on general interfaces:

A minor selection of related issues:

As proposed in ticket #57644 we use the regular generic syntax, however you must write it in an ignored source code file. If invoked, the choicgen tool parses all files with the following build tags:

//go:build choice

package mypkg

// MyChoiceType documentation is copied over.
type MyChoiceType interface {
	// optionally error is supported which creates a simple boilerplate (Error string) method.
    Inc | Dec | None
}

Then, it generates a choicetypes.gen.go files, which contains all the boilerplate, if you declare a standard go:generate directive like:

//go:generate go run github.com/worldiety/choicegen@latest

Example

See the example package.

What about a tagged union?

E.g. Rust implements its enum type using a low level tagged union, but questions about how the precise GC should work in Go, arise. One could still use an interface, but that double boxing does not make much sense and breaks the Go interface polymorphism entirely. We experimented with pre-generated generic (tuple like) union types, which results in a lot of mapping boilerplate, which naturally disappears when using plain interfaces.

Limitations

Most notably are the following limitations of the current approach:

  • a real closed sum type cannot be expressed in Go and using type embedding, there may be awkward situations
  • because the generator creates a bunch of marker methods, this kind of modelling is only applicable for named package local types
  • a sum type can be always nil, even though its members never can't.
  • everything escapes to the heap (as always with regular interfaces).

About

This Go module provides a boilerplate generator for modelling compiler checked tagged union / sum types / choice types

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages