refactor: naming
This commit is contained in:
@@ -2,34 +2,34 @@ package typeutils
|
||||
|
||||
// Created to abstract over Is_some and Is_ok
|
||||
type ValueContainer interface {
|
||||
Has_value() bool
|
||||
HasValue() 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
|
||||
Expect(string) T // panics with a provided custom message
|
||||
Unwrap() T // panics with a generic message
|
||||
UnwrapOr(T) T // returns the provided default value
|
||||
UnwrapOrDefault() T // returns the default value of the type T
|
||||
UnwrapOrElse(func() T) T // returns the result of evaluating the provided function
|
||||
}
|
||||
|
||||
// Both an Optional and Result is an Option
|
||||
type Option[T any] interface {
|
||||
// Both an Optional and Result is an Optioner
|
||||
type Optioner[T any] interface {
|
||||
ValueContainer
|
||||
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 Optionaler[T any] interface {
|
||||
IsSome() bool
|
||||
IsNone() bool
|
||||
OkOr(error) Result[T]
|
||||
OkOrElse(func() error) Result[T]
|
||||
Unwrappable[T]
|
||||
}
|
||||
|
||||
type ResultI[T any] interface {
|
||||
Is_ok() bool
|
||||
Is_err() bool
|
||||
type Resulter[T any] interface {
|
||||
IsOk() bool
|
||||
IsErr() bool
|
||||
Ok() Optional[T]
|
||||
Err() Optional[error]
|
||||
Unwrappable[T]
|
||||
@@ -37,10 +37,10 @@ type ResultI[T any] interface {
|
||||
|
||||
// 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)
|
||||
_ Optioner[any] = (*Optional[any])(nil)
|
||||
_ Optioner[any] = (*Result[any])(nil)
|
||||
_ Optionaler[any] = (*Optional[any])(nil)
|
||||
_ Resulter[any] = (*Result[any])(nil)
|
||||
_ ValueContainer = (*Optional[any])(nil)
|
||||
_ ValueContainer = (*Result[any])(nil)
|
||||
)
|
||||
|
||||
@@ -16,10 +16,10 @@ func None_t[T any](T) Optional[T] {
|
||||
|
||||
// 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]) IsSome() bool { return opt.present }
|
||||
func (opt Optional[T]) IsNone() bool { return !opt.present }
|
||||
|
||||
func (opt Optional[T]) Has_value() bool { return opt.Is_some() }
|
||||
func (opt Optional[T]) HasValue() bool { return opt.IsSome() }
|
||||
|
||||
// UNWRAPPABLE INTERFACE BEGIN
|
||||
func (opt Optional[T]) Expect(msg string) T {
|
||||
@@ -36,14 +36,14 @@ func (opt Optional[T]) Unwrap() T {
|
||||
panic("Tried unwrapping an Optional that did not have a value!")
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or(val T) T {
|
||||
func (opt Optional[T]) UnwrapOr(val T) T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or_default() T {
|
||||
func (opt Optional[T]) UnwrapOrDefault() T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func (opt Optional[T]) Unwrap_or_default() T {
|
||||
return res
|
||||
}
|
||||
|
||||
func (opt Optional[T]) Unwrap_or_else(f func() T) T {
|
||||
func (opt Optional[T]) UnwrapOrElse(f func() T) T {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func (opt Optional[T]) Unwrap_or_else(f func() T) T {
|
||||
// UNWRAPPABLE INTERFACE END
|
||||
|
||||
// transforms Some(v) to Ok(v), and None to Err(err)
|
||||
func (opt Optional[T]) Ok_or(err error) Result[T] {
|
||||
func (opt Optional[T]) OkOr(err error) Result[T] {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func (opt Optional[T]) Ok_or(err error) Result[T] {
|
||||
}
|
||||
|
||||
// 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] {
|
||||
func (opt Optional[T]) OkOrElse(f func() error) Result[T] {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
|
||||
@@ -15,10 +15,10 @@ func Err_t[T any](err error, x T) Result[T] {
|
||||
|
||||
// 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]) IsOk() bool { return res.err == nil }
|
||||
func (res Result[T]) IsErr() bool { return res.err != nil }
|
||||
|
||||
func (res Result[T]) Has_value() bool { return res.Is_ok() }
|
||||
func (res Result[T]) HasValue() bool { return res.IsOk() }
|
||||
|
||||
// UNWRAPPABLE INTERFACE
|
||||
func (res Result[T]) Expect(msg string) T {
|
||||
@@ -35,14 +35,14 @@ func (res Result[T]) Unwrap() T {
|
||||
panic("Tried unwrapping a Result that had an error value!")
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or(val T) T {
|
||||
func (res Result[T]) UnwrapOr(val T) T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or_default() T {
|
||||
func (res Result[T]) UnwrapOrDefault() T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (res Result[T]) Unwrap_or_default() T {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (res Result[T]) Unwrap_or_else(f func() T) T {
|
||||
func (res Result[T]) UnwrapOrElse(f func() T) T {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (res Result[T]) Unwrap_or_else(f func() T) T {
|
||||
// UNWRAPPABLE INTERFACE
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
func (res Result[T]) Expect_err(msg string) error {
|
||||
func (res Result[T]) ExpectErr(msg string) error {
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func (res Result[T]) Expect_err(msg string) error {
|
||||
}
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
func (res Result[T]) Unwrap_err() error {
|
||||
func (res Result[T]) UnwrapErr() error {
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
|
||||
@@ -8,21 +8,21 @@ 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 UnwrapOr[T any](val Unwrappable[T]) (def T) {
|
||||
return val.UnwrapOr(def)
|
||||
}
|
||||
|
||||
func Unwrap_or_default[T any](val Unwrappable[T]) T {
|
||||
return val.Unwrap_or_default()
|
||||
func UnwrapOrDefault[T any](val Unwrappable[T]) T {
|
||||
return val.UnwrapOrDefault()
|
||||
}
|
||||
|
||||
func Unwrap_or_else[T any](val Unwrappable[T], f func() T) T {
|
||||
return val.Unwrap_or_else(f)
|
||||
func UnwrapOrElse[T any](val Unwrappable[T], f func() T) T {
|
||||
return val.UnwrapOrElse(f)
|
||||
}
|
||||
|
||||
// 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()
|
||||
func HasValue(val ValueContainer) bool {
|
||||
return val.HasValue()
|
||||
}
|
||||
|
||||
func ResultWrap[T any](val T, err error) Result[T] {
|
||||
|
||||
Reference in New Issue
Block a user