GithubHelp home page GithubHelp logo

choonkeat / sumtype-go Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 43 KB

Fastest and simplest pattern matching sum types in Go. Don't be jealous of Rust anymore.

License: MIT License

Go 98.24% Makefile 1.76%
result-type sumtypes union-types algebraic-data-types codegenerator golang tagged-unions

sumtype-go's Introduction

sumtype-go

Introduction

sumtype-go is a CLI tool designed to facilitate the creation and management of sum types (aka union types) in Go. This tool simplifies the process of generating boilerplate code for discriminated union types, making it easier to work with its variants in Go.

Quick Tour

To define this sum type:

type User
    = Anonymous
    | Member String Time
    | Admin String

write this Go type definition, e.g. in declaration.go

type User interface {
	Match(s UserVariants)
}

type UserVariants struct { // be sure to suffix the name with `Variants`
	Anonymous func()
	Member    func(email string, since time.Time)
	Admin     func(email string)
}

Execute this command

go install github.com/choonkeat/[email protected] # to install
sumtype-go -input declaration.go

To generate declaration.boilerplate.go and start using it!

users := []User{
	Anonymous(),                 // this returns a `User` value
	Member("Alice", time.Now()), // this also returns a `User` value
	Admin("Bob"),                // this also returns a `User` value
}

and we can write functions that work with User

func UserString(u User) string {
	var result string
	u.Match(UserVariants{
		Anonymous: func() {
			result = "Anonymous coward"
		},
		Member: func(email string, since time.Time) {
			result = email + " (member since " + since.String() + ")"
		},
		Admin: func(email string) {
			result = email + " (admin)"
		},
	})
	return result
}

Refer to example/gosumtype_1_*.go

Generics

We support generics too. e.g. the classic Result type

type Result x a
    = Err x
    | Ok a

can be defined as

type Result[x, a any] interface {
	Match(s ResultVariants[x, a])
}

type ResultVariants[x, a any] struct {
	Err func(err x)
	Ok  func(data a)
}

Same thing, after executing sumtype-go to generate the .boilerplate.go file, you can use it like

results := []Result[string, int]{
	Err[string, int]("Oops err"), // this returns a `Result` value
	Ok[string, int](42),          // this also returns a `Result` value
}

for i, result := range results {
	HandleResult(i, result) // implement your own `func HandleResult(int, Result[string, int])`
}

Refer to example/result_1_*.go

Installation

To install sumtype-go, ensure you have Go installed on your system, and then run the following command:

go install github.com/choonkeat/[email protected]

Usage

After installation, you can start using sumtype-go by invoking it from the command line.

$ sumtype-go -h
Usage of sumtype-go:
  -input string
    	Input file name
  -pattern-match string
    	Name of the pattern match method (default "Match")
  -suffix string
    	Suffix of the struct defining variants (default "Variants")

Here's a basic example of how to use it:

sumtype-go -input example/gosumtype_1_declaration.go

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.