feat(Type): add new function to be able to recover from panics when unwrapping a none/error value

This commit is contained in:
2025-01-20 14:18:43 +01:00
parent 286ae7f28b
commit cb32017224
4 changed files with 131 additions and 6 deletions

View File

@@ -12,6 +12,9 @@ import (
Assert "github.com/lbatuska/goutils/assert"
)
// Marker interface impl
func (opt Optional[T]) Optional() {}
// CTORS BEGIN
func Some[T any](value T) Optional[T] {
return Optional[T]{value, true}
@@ -57,11 +60,14 @@ func (opt Optional[T]) Expect(msg string) T {
panic(msg)
}
func (opt Optional[T]) Unwrap() T {
func (opt *Optional[T]) Unwrap() T {
if opt == nil {
panic("Tried unwrapping an Optional that did not have a value!")
}
if opt.present {
return opt.value
}
panic("Tried unwrapping an Optional that did not have a value!")
panic(opt)
}
func (opt *Optional[T]) UnwrapOr(val T) T {