Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

rodrigocfd/sum-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sum Types

⚠️ Note: This project is purely experimental, and it's currently archived.

This library is an attempt to emulate sum types using Go generics.

Package Description
opt.Optional Optional type.
st2.SumType2 Sum type for 2 types.
st3.SumType3 Sum type for 3 types.

Sum type example

Let's create a sum type variable which can hold an int or a string. In such case, int will be our type 0, and string will be our type 1. To initialize the variable with a string value, which is our type 1, we call the New1() function:

nameOrAge := st2.New1[int, string]("John")

Attempting to retrieve the string value with the Get1() method:

if name, ok := nameOrAge.Get1(); ok {
    fmt.Printf("Name is %s.\n", name)
}

To reassign this variable to an int, which is our type 0, we call the New0() function. And we attempt to read it with the Get0() method:

nameOrAge = st2.New0[int, string](40)

if age, ok := nameOrAge.Get0(); ok {
    fmt.Printf("Age is %d.\n", age)
}

Exhaustive pattern matching can be performed by passing callback functions to the Match() method:

nameOrAge.Match(
    func(age int) {
        fmt.Printf("Age is %d.\n", age)
    },
    func(name string) {
        fmt.Printf("Name is %s.\n", name)
    },
)

For sum type structs with more fields, the logic is the same: st3 exposes functions New0(), New1() and New2(), and so on.

Optional example

Let's create an optional string:

maybeName := opt.Some[string]("John")

Attempting to retrieve the value:

if name, ok := maybeName.Get(); ok {
    fmt.Printf("Name is %s.\n", name)
} else {
    fmt.Println("No name.")
}

Reassigning to an empty value, and checking it:

maybeName = opt.None[string]()

if !maybeName.IsSome() {
    fmt.Println("No name.")
}

License

Licensed under MIT license, see LICENSE.md for details.