initial option and result

This commit is contained in:
2025-06-06 09:59:12 -04:00
committed by Zachary King
parent 039ca3b6f0
commit 7239eca2e5
3 changed files with 90 additions and 0 deletions

40
result.go Normal file
View File

@@ -0,0 +1,40 @@
package util
type Result[T any] struct {
value *T
err error
}
func ResultOk[T any](value T) Result[T] {
return Result[T]{value: &value, err: nil}
}
func ResultErr[T any](err error) Result[T] {
return Result[T]{value: nil, err: err}
}
func (res Result[T]) IsOk() bool {
return res.err == nil
}
func (res Result[T]) IsErr() bool {
return !res.IsOk()
}
func (res Result[T]) Value() T {
if !res.IsOk() {
panic("Yank on Err Result")
}
return *res.value
}
func (res Result[T]) Error() error {
if !res.IsErr() {
panic("Yank on OK Result")
}
return res.err
}