inital commit
This commit is contained in:
43
typeutils/interfaces.go
Normal file
43
typeutils/interfaces.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package typeutils
|
||||
|
||||
// Created to abstract over Is_some and Is_ok
|
||||
type ValueContainer interface {
|
||||
Has_value() bool
|
||||
}
|
||||
|
||||
type Unwrappable[T any] interface {
|
||||
Expect(string) T // panics with a provided custom message
|
||||
Unwrap() T // panics with a generic message
|
||||
Unwrap_or(T) T // returns the provided default value
|
||||
Unwrap_or_default() T // returns the default value of the type T
|
||||
Unwrap_or_else(func() T) T // returns the result of evaluating the provided function
|
||||
}
|
||||
|
||||
// Both an Optional and Result is an Option
|
||||
type Option[T any] interface {
|
||||
Unwrappable[T]
|
||||
}
|
||||
|
||||
type OptionalI[T any] interface {
|
||||
Is_some() bool
|
||||
Is_none() bool
|
||||
Ok_or(error) Result[T]
|
||||
Ok_or_else(func() error) Result[T]
|
||||
}
|
||||
|
||||
type ResultI[T any] interface {
|
||||
Is_ok() bool
|
||||
Is_err() bool
|
||||
Ok() Optional[T]
|
||||
Err() Optional[error]
|
||||
}
|
||||
|
||||
// Ensure compile time the interfaces are implemented
|
||||
var (
|
||||
_ Option[any] = (*Optional[any])(nil)
|
||||
_ Option[any] = (*Result[any])(nil)
|
||||
_ OptionalI[any] = (*Optional[any])(nil)
|
||||
_ ResultI[any] = (*Result[any])(nil)
|
||||
_ ValueContainer = (*Optional[any])(nil)
|
||||
_ ValueContainer = (*Result[any])(nil)
|
||||
)
|
||||
77
typeutils/optional.go
Normal file
77
typeutils/optional.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package typeutils
|
||||
|
||||
// CTORS BEGIN
|
||||
func Some[T any](value T) Optional[T] {
|
||||
return Optional[T]{value, true}
|
||||
}
|
||||
|
||||
func None[T any]() Optional[T] {
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// Because None has no type passing a value of desired type might be preferred syntax over providing type on the function call
|
||||
func None_t[T any](T) Optional[T] {
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// CTORS END
|
||||
|
||||
func (opt Optional[T]) Is_some() bool { return opt.present }
|
||||
func (opt Optional[T]) Is_none() bool { return !opt.present }
|
||||
|
||||
func (opt Optional[T]) Has_value() bool { return opt.Is_some() }
|
||||
|
||||
// UNWRAPPABLE INTERFACE BEGIN
|
||||
func (opt Optional[T]) Expect(msg string) T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap() T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
panic("Tried unwrapping an Optional that did not have a value!")
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or(val T) T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or_default() T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
var res T
|
||||
return res
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or_else(f func() T) T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
// UNWRAPPABLE INTERFACE END
|
||||
|
||||
// transforms Some(v) to Ok(v), and None to Err(err)
|
||||
func (opt Optional[T]) Ok_or(err error) Result[T] {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
return Err[T](err)
|
||||
}
|
||||
|
||||
// transforms Some(v) to Ok(v), and None to a value of Err using the provided function
|
||||
func (opt Optional[T]) Ok_or_else(f func() error) Result[T] {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
return Err[T](f())
|
||||
}
|
||||
92
typeutils/result.go
Normal file
92
typeutils/result.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package typeutils
|
||||
|
||||
// CTORS BEGIN
|
||||
func Ok[T any](value T) Result[T] {
|
||||
return Result[T]{value: value, err: nil}
|
||||
}
|
||||
|
||||
func Err[T any](err error) Result[T] {
|
||||
return Result[T]{err: err}
|
||||
}
|
||||
|
||||
func Err_t[T any](err error, x T) Result[T] {
|
||||
return Result[T]{err: err}
|
||||
}
|
||||
|
||||
// CTORS END
|
||||
|
||||
func (res Result[T]) Is_ok() bool { return res.err == nil }
|
||||
func (res Result[T]) Is_err() bool { return res.err != nil }
|
||||
|
||||
func (res Result[T]) Has_value() bool { return res.Is_ok() }
|
||||
|
||||
// UNWRAPPABLE INTERFACE
|
||||
func (res Result[T]) Expect(msg string) T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap() T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
panic("Tried unwrapping a Result that had an error a value!")
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or(val T) T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or_default() T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
var ret T
|
||||
return ret
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or_else(f func() T) T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
// UNWRAPPABLE INTERFACE
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
func (res Result[T]) Expect_err(msg string) error {
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
func (res Result[T]) Unwrap_err() error {
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
panic("Expect_err was called with an Ok value")
|
||||
}
|
||||
|
||||
// transforms Result into Option, mapping Ok(v) to Some(v) and Err(e) to None
|
||||
func (res Result[T]) Ok() Optional[T] {
|
||||
if res.err == nil {
|
||||
return Optional[T]{value: res.value, present: true}
|
||||
}
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// transforms Result into Option, mapping Err(e) to Some(e) and Ok(v) to None
|
||||
func (res Result[T]) Err() Optional[error] {
|
||||
if res.err != nil {
|
||||
return Optional[error]{value: res.err, present: true}
|
||||
}
|
||||
return Optional[error]{present: false}
|
||||
}
|
||||
11
typeutils/types.go
Normal file
11
typeutils/types.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package typeutils
|
||||
|
||||
type Optional[T any] struct {
|
||||
value T
|
||||
present bool
|
||||
}
|
||||
|
||||
type Result[T any] struct {
|
||||
value T
|
||||
err error
|
||||
}
|
||||
27
typeutils/utils.go
Normal file
27
typeutils/utils.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package typeutils
|
||||
|
||||
func Expect[T any](val Unwrappable[T], msg string) T {
|
||||
return val.Expect(msg)
|
||||
}
|
||||
|
||||
func Unwrap[T any](val Unwrappable[T]) T {
|
||||
return val.Unwrap()
|
||||
}
|
||||
|
||||
func Unwrap_or[T any](val Unwrappable[T]) (def T) {
|
||||
return val.Unwrap_or(def)
|
||||
}
|
||||
|
||||
func Unwrap_or_default[T any](val Unwrappable[T]) T {
|
||||
return val.Unwrap_or_default()
|
||||
}
|
||||
|
||||
func Unwrap_or_else[T any](val Unwrappable[T], f func() T) T {
|
||||
return val.Unwrap_or_else(f)
|
||||
}
|
||||
|
||||
// T cannot be inferred
|
||||
// Returns if the underlying data has a Value (false in case of None or Error)
|
||||
func Has_value(val ValueContainer) bool {
|
||||
return val.Has_value()
|
||||
}
|
||||
Reference in New Issue
Block a user