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 ¶
- type Add
- type ComparableFields
- type Fields
- type Int
- func (enum Int[Sum]) Case(val Int[Sum], fn func()) Int[Sum]
- func (enum Int[Sum]) MarshalJSON() ([]byte, error)
- func (enum Int[Sum]) MarshalText() ([]byte, error)
- func (enum Int[Sum]) String() string
- func (enum Int[Sum]) Sum() Sum
- func (enum Int[Sum]) Switch(fields Fields, cases Sum)
- func (enum Int[Sum]) UmarshalJSON(data []byte) error
- func (enum *Int[Sum]) UnmarshalText(text []byte) error
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Add ¶
Add is used to add fixed types to the Fields of a Type.
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 ¶
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 ¶
MarshalJSON implements json.Marshaler.
func (Int[Sum]) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
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 ¶
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 ¶
MarshalJSON implements json.Unmarshaler.
func (*Int[Sum]) UnmarshalText ¶
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 ¶
MarshalJSON implements json.Marshaler.
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 ¶
MarshalJSON implements json.Unmarshaler.