types

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 2 Imported by: 13

README

Go Types

Provides Option, Result and Tuple generic types for go and supporting methods

Tuple

Tuples represent 2 or more values as a single type. They are very helpful when creating generic go code that utilizes function signatures.

For full examples of the tuple types see the tuple tests

Tuple[T1, T2 any]

Represents 2 values in a single type.

The easiest way to create a Tuple[T1, T2 any] is to use the tuple package.

package main

import(
    "fmt"
    "github.com/patrickhuber/go-types/tuple"
)
func main(){
    t := tuple.New2(1, "hello world")
    i, s := t.Deconstruct()
    fmt.Printf("%d %s", i, s)
}

Go Playground

Tuple3[T1, T2, T3 any]

Represents 3 values in a single type.

The easiest way to create a Tuple3[T1, T2, T3 any] is to use the tuple package

package main

import(
    "fmt"
    "github.com/patrickhuber/go-types/tuple"
)
func main(){
    t := tuple.New3(1, "hello world", 1.234)
    i, s, f := t.Deconstruct()
    fmt.Printf("%d %s %f", i, s, f)
}

Go Playground

Tuple4[T1, T2, T3, T4 any]

Represents 4 values in a single type.

The easiest way to create a Tuple4[T1, T2, T3, T4 any] is to use the tuple package

package main

import (
	"fmt"

	"github.com/patrickhuber/go-types/tuple"
)

func main() {
	t := tuple.New4(1, "hello world", 1.234, true)
	i, s, f, b := t.Deconstruct()
	fmt.Printf("%d %s %f %t", i, s, f, b)
}

Go Playground

Option[T any]

Option[T any] represents Something or Nothing.

The core types for the option type are in the types pacakge. Helper functions for creating options are located in the option package.

For full examples of the option types see the option tests

Some[T any]

Some[T any] is an Option[T any] with a value.

The easiest way to create a Some type is to use the option package

package main

import (
    "github.com/patrickhuber/go-types/option"
    "github.com/patrickhuber/go-types"
    "fmt"
)
func main(){
    // creates a types.Some[int] using type inference
    op := option.Some(1) 
    // because Some and None are types, a types switch can be used
    switch o := op.(type){
    case types.Some[int]:
        fmt.Println("some", o.Value())
    case types.None[int]:
        fmt.Println("none")
    }    
}

Go Playground

None[T any]

None[T any] is an Option[T any] with no value

The easiest way to create a None type is to use the option package

package main

import (
    "github.com/patrickhuber/go-types/option"
    "github.com/patrickhuber/go-types"
    "fmt"
)
func main(){
    // creates a types.None[int]
    op := option.None[int]() 
    // because Some and None are types, a types switch can be used
    switch o := op.(type){
    case types.Some[int]:
        fmt.Println("some", o.Value())
    case types.None[int]:
        fmt.Println("none")
    }    
}

Go Playground

Result[T any]

Result[T any] represents a value Ok or an Error

The core types for result type are in the types package. Helper functions for creating results are located in the result package.

For full examples of the result types see the result tests

Ok[T any]

Ok[T any] is a successful Result[T any] with a value

The easiest way to create a Ok type is to use the result package

package main

import (
    "github.com/patrickhuber/go-types/result"
    "github.com/patrickhuber/go-types"
    "fmt"
)
func main(){
    // creates a Ok[int] result using type inference
    res := result.Ok(1) 
    // because Ok and Error are types, a types switch can be used
    switch r := res.(type){
    case types.Ok[int]:
        fmt.Println("value", r.Ok())
    case types.Error[int]:
        fmt.Println(r.Error())
    }    
}

Go Playground

Error[T any]

Error[T any] is a failed Result[T any] with an error

The easiest way to create an Error type is to use the result package

package main

import (
    "github.com/patrickhuber/go-types/result"
    "github.com/patrickhuber/go-types"
    "fmt"
)
func main(){
    // creates a Error[int] result
    res := result.Errorf[int]("error")

    // because Ok and Error are types, a types switch can be used
    switch r := res.(type){
    case types.Ok[int]:
        fmt.Println("value", r.Ok())
    case types.Error[int]:
        fmt.Println(r.Error())
    }    
}

Go Playground

Error Handling

Result[T any] can be used to avoid if err != nil repetition in code by passing in Result[T any] objects instead of values

Baseline

As a baseline look at the idiomatic go example

Assert

To utilize the library without modifying dependencies you can use defer handle.Error(&res) along with the assert package to remove if err != nil { return err} checks. The assert example shows how.

Wrapping

To use results in return types by wrapping existing functions that return errors see the wrap example

The result.Unwrap() syntax is very similar to rust's ? syntax and dramatically reduces the verticle size of the code while still handing errors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error[T any] interface {
	Result[T]
	Error() error
}

type None

type None[T any] interface {
	Option[T]
	// contains filtered or unexported methods
}

None represents an Option[T] that does not have a value

type Ok

type Ok[T any] interface {
	Result[T]
	Ok() T
}

type Option

type Option[T any] interface {

	// Deconstruct returns Value, true if the underlying type is Some
	// returns Default, false if the underlying type is None
	Deconstruct() (T, bool)
	// IsSome returns true if the underlying type is Some
	IsSome() bool
	// IsNone returns true if the underlying type is None
	IsNone() bool
	// Unwrap unwraps the Option to its underlying value if the type is Some and
	// panics if the type is None
	Unwrap() T
	// contains filtered or unexported methods
}

Option represents a value or nothing

func NewNone

func NewNone[T any]() Option[T]

func NewSome

func NewSome[T any](value T) Option[T]

type Result

type Result[T any] interface {

	// Deconstruct expands the result to its underlying values.
	// if the result is an error, T contains the zero value.
	Deconstruct() (T, error)

	// IsOk returns true for Ok results, false for Error results.
	IsOk() bool

	// IsError returns false for Ok results, true for Error results.
	// IsError accepts a list of errors and errors.Is() matches the internal error, returns true, false otherwise.
	IsError(err ...error) bool

	// Unwrap attempts to unwrap the result to its value.
	// If the result is an Error, Unwrap will panic.
	// Unwrap is meant to be used with handle.Error(*types.Result)
	Unwrap() T
	// contains filtered or unexported methods
}

Result represents a value or an error

func Cast

func Cast[TSource, TTarget any](source TSource) Result[TTarget]

func Castf

func Castf[TSource, TTarget any](source TSource, format string, args ...any) Result[TTarget]

func NewError

func NewError[T any](value error) Result[T]

func NewOk

func NewOk[T any](value T) Result[T]

func NewResult

func NewResult[T any](ok T, err error) Result[T]

NewResult returns a Error if err is not nil and Ok if err is nil. Most consumers of the module will use the `result` package `Ok` and `Error` functions instead.

type Some

type Some[T any] interface {
	// must implement Option[T]
	Option[T]
	// Value returns the underlying value
	Value() T
}

Some represents an Option[T] that has a value

type Tuple2

type Tuple2[T1, T2 any] interface {
	Deconstruct() (T1, T2)
	Value1() T1
	Value2() T2
	// contains filtered or unexported methods
}

func NewTuple2

func NewTuple2[T1, T2 any](t1 T1, t2 T2) Tuple2[T1, T2]

type Tuple3

type Tuple3[T1, T2, T3 any] interface {
	Deconstruct() (T1, T2, T3)
	Value1() T1
	Value2() T2
	Value3() T3
	// contains filtered or unexported methods
}

func NewTuple3

func NewTuple3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3) Tuple3[T1, T2, T3]

type Tuple4

type Tuple4[T1, T2, T3, T4 any] interface {
	Deconstruct() (T1, T2, T3, T4)
	Value1() T1
	Value2() T2
	Value3() T3
	Value4() T4
	// contains filtered or unexported methods
}

func NewTuple4

func NewTuple4[T1, T2, T3, T4 any](t1 T1, t2 T2, t3 T3, t4 T4) Tuple4[T1, T2, T3, T4]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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