sum

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package sum provides sum types / enumerated types / tagged unions / variants / discriminated unions.

Sum Types

type SVGPathCommand struct {
	Line       sum.Add[SVGPathCommand, struct{X float64; Y float64}]
	Horizontal sum.Add[SVGPathCommand, float64]
	Vertical   sum.Add[SVGPathCommand, float64]
}

var SVGPathCommands = sum.Type[SVGPathCommand]{}.Sum()

var command = SVGPathCommands.Horizontal.New(22)

command.Switch(SVGPathCommands{
	sum.Switch(command, func(line struct{X float64; Y float64}) {
		// draw line
	}),
	sum.Switch(command, func(horizontal float64) {
		// move horizontally
	}),
	sum.Switch(command, func(vertical float64) {
		// move vertically
	}),
})

Enumerated Sum Types

type Weekday struct {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday sum.Int[Weekday]
}

var Weekdays = sum.Int[Weekday]{}.Sum()

var day sum.Int[Weekday]
day = Weekdays.Monday

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Add

type Add[Sum Fields, T any] struct {
	// contains filtered or unexported fields
}

Add is used to add fixed types to the Fields of a Type.

func Case

func Case[Sum any, T any](val Type[Sum], fn func(T)) Add[Sum, T]

Case can be passed to a switch to run a function when the associated type in the sum type is matched. The val passed to this function should refer to the value being switched on.

func (Add[Sum, T]) New

func (field Add[Sum, T]) New(val T) Type[Sum]

New returns a value of Type[Sum] with a value matching the type this Add corresponds to.

type ComparableFields

type ComparableFields interface {
	comparable
}

ComparableFields is a named struct that describes an enumerated sum type, each field adds a distinct value to the enumerated set. The size and configuration of the underlying value is by convention defined on the last field in the structure.

type Weekdays struct {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday sum.Int[Weekdays]
}

The first field is considered the zero value.

type Fields

type Fields any
	Fields is a named struct that describes a sum type, each field
 	may Add a type to the sum, for example:

		type MySumType struct {
			Int    sum.Add[MySumType, Int]
			String sum.Add[MySumType, String]
		}

	The first sum.Add field will be the type of the zero value.
	Any field that is not a sum.Add, will be ignored by the sum
	package. Multiple fields may be added to the same type, they
	are distinguished by their name.

type Int

type Int[Sum ComparableFields] struct {
	// contains filtered or unexported fields
}

Int is a special kind of sum type, where each field is untyped. The underlying value is stored as an int.

func (Int[Sum]) Case

func (enum Int[Sum]) Case(val Int[Sum], fn func()) Int[Sum]

Case evaulates that that the enumerated value is equal to the given case, if it is, the provided function is executed.

func (Int[Sum]) MarshalJSON

func (enum Int[Sum]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Int[Sum]) MarshalText

func (enum Int[Sum]) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Int[Sum]) String

func (enum Int[Sum]) String() string

String implements fmt.Stringer.

func (Int[Sum]) Sum

func (enum Int[Sum]) Sum() Sum

Sum returns a set of fields that can be used to set the value of an enumerated sum type, you may wish to initialise this as a global variable to cache the result.

func (Int[Sum]) Switch

func (enum Int[Sum]) Switch(fields Fields, cases Sum)

Switch can be used to create an O(N) exaustive switch on this value by providing unnamed cases. To perform a non-exhaustive switch, use the builtin switch statement instead, which will likely be O(1).

func (Int[Sum]) UmarshalJSON

func (enum Int[Sum]) UmarshalJSON(data []byte) error

MarshalJSON implements json.Unmarshaler.

func (*Int[Sum]) UnmarshalText

func (enum *Int[Sum]) UnmarshalText(text []byte) error

UmarshalText implements encoding.TextUnmarshaler.

type Type

type Type[Sum Fields] struct {
	// contains filtered or unexported fields
}

Type is a sum type / discriminated union / variant type the type parameter should refer to a named Fields struct with sum.Add fields that add to the sum of types.

func (Type[Sum]) MarshalJSON

func (sum Type[Sum]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Type[Sum]) String

func (sum Type[Sum]) String() string

String implements fmt.Stringer.

func (Type[Sum]) Sum

func (Type[Sum]) Sum() Sum

Sum returns a set of fields that can be used to set the value of a sum type, you may wish to initialise this as a global variable to cache the result.

func (Type[Sum]) Switch

func (val Type[Sum]) Switch(cases Sum)

Switch can be used to read the value of a sum type, the cases can should be added with Case calls. If the cases are named, then this is a non-exhaustive switch. If the cases are not named, then the switch is exhaustive. You can force a type switch to be non exhaustive by adding a _ struct{} field to the Fields of the sum type.

func (*Type[Sum]) UmarshalJSON

func (sum *Type[Sum]) UmarshalJSON(data []byte) error

MarshalJSON implements json.Unmarshaler.

Jump to

Keyboard shortcuts

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