fix: minor fixes to types

This commit is contained in:
2024-09-15 18:26:32 +02:00
parent 2c75b20349
commit 4d974d086f
2 changed files with 17 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ type Unwrappable[T any] interface {
// Both an Optional and Result is an Option // Both an Optional and Result is an Option
type Option[T any] interface { type Option[T any] interface {
ValueContainer
Unwrappable[T] Unwrappable[T]
} }
@@ -23,6 +24,7 @@ type OptionalI[T any] interface {
Is_none() bool Is_none() bool
Ok_or(error) Result[T] Ok_or(error) Result[T]
Ok_or_else(func() error) Result[T] Ok_or_else(func() error) Result[T]
Unwrappable[T]
} }
type ResultI[T any] interface { type ResultI[T any] interface {
@@ -30,6 +32,7 @@ type ResultI[T any] interface {
Is_err() bool Is_err() bool
Ok() Optional[T] Ok() Optional[T]
Err() Optional[error] Err() Optional[error]
Unwrappable[T]
} }
// Ensure compile time the interfaces are implemented // Ensure compile time the interfaces are implemented

View File

@@ -20,8 +20,21 @@ func Unwrap_or_else[T any](val Unwrappable[T], f func() T) T {
return val.Unwrap_or_else(f) 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) // Returns if the underlying data has a Value (false in case of None or Error)
func Has_value(val ValueContainer) bool { func Has_value(val ValueContainer) bool {
return val.Has_value() return val.Has_value()
} }
func ResultWrap[T any](val T, err error) Result[T] {
if err == nil {
return Ok(val)
}
return Err[T](err)
}
func ResultWrapb[T any](err error, val T) Result[T] {
if err == nil {
return Ok(val)
}
return Err[T](err)
}