fn: add conversions between Result and Option

This commit is contained in:
Keagan McClelland
2024-08-06 15:38:14 -07:00
parent a741c54175
commit 5faad77f33
4 changed files with 58 additions and 0 deletions

View File

@@ -79,15 +79,29 @@ func (r Result[T]) MapErr(f func(error) error) Result[T] {
}
// Option returns the success value as an Option.
//
// Deprecated: Use OkToSome instead.
func (r Result[T]) Option() Option[T] {
return r.Either.LeftToOption()
}
// OkToSome mutes the error value of the result.
func (r Result[T]) OkToSome() Option[T] {
return r.Either.LeftToOption()
}
// WhenResult executes the given function if the Result is a success.
//
// Deprecated: Use WhenOk instead.
func (r Result[T]) WhenResult(f func(T)) {
r.WhenLeft(f)
}
// WhenOk executes the given function if the Result is a success.
func (r Result[T]) WhenOk(f func(T)) {
r.WhenLeft(f)
}
// WhenErr executes the given function if the Result is an error.
func (r Result[T]) WhenErr(f func(error)) {
r.WhenRight(f)