Result
extends
Monad
in
Result monad as a `Either monad`.
Inspired by Rust's Result enum.
Tags
Attributes
- #[Sealed]
- $permits: [\WizDevelop\PhpMonad\Result\Ok::class, \WizDevelop\PhpMonad\Result\Err::class]
Table of Contents
Methods
- and() : Result<U, E>
- andThen() : E>)
- NOTE: PHPdoc's completion by type specification in Closure doesn't work, so I'm redefining it.
- err() : Option<string|int, E>
- expect() : T
- inspect() : $this
- inspectErr() : $this
- isErr() : bool
- isErrAnd() : bool
- isOk() : bool
- isOkAnd() : bool
- map() : Result<U, E>
- mapErr() : Result<T, F>
- mapOr() : U
- mapOrElse() : U
- match() : U|V
- Applies one of two functions depending on whether the result is Ok or Err.
- ok() : Option<string|int, T>
- or() : Result<T, F>
- orElse() : Result<T, F>
- orThrow() : $this
- Returns the contained Ok value or throws the provided exception.
- unit() : Monad<string|int, TValue>
- `return` in Haskell. (`Unit` operation.)
- unwrap() : T
- unwrapErr() : E
- unwrapOr() : T|U
- unwrapOrElse() : T|U
- unwrapOrThrow() : T
- Returns the contained Ok value or throws the provided exception.
Methods
and()
public
and(Result<U, E> $right) : Result<U, E>
Parameters
- $right : Result<U, E>
Tags
Return values
Result<U, E>andThen()
NOTE: PHPdoc's completion by type specification in Closure doesn't work, so I'm redefining it.
public
andThen(callable(T): Result<U, F> $right) : E>)
Parameters
- $right : callable(T): Result<U, F>
Tags
Return values
E>)err()
public
err() : Option<string|int, E>
Tags
Return values
Option<string|int, E>expect()
public
expect(string $message) : T
Parameters
- $message : string
Tags
Return values
Tinspect()
public
inspect(callable(T): mixed $callback) : $this
Parameters
- $callback : callable(T): mixed
Tags
Return values
$thisinspectErr()
public
inspectErr(callable(E): mixed $callback) : $this
Parameters
- $callback : callable(E): mixed
Tags
Return values
$thisisErr()
public
isErr() : bool
Tags
Return values
boolisErrAnd()
public
isErrAnd(callable(E): bool $predicate) : bool
Parameters
- $predicate : callable(E): bool
Tags
Return values
boolisOk()
public
isOk() : bool
Tags
Return values
boolisOkAnd()
public
isOkAnd(callable(T): bool $predicate) : bool
Parameters
- $predicate : callable(T): bool
Tags
Return values
boolmap()
public
map(callable(T): U $callback) : Result<U, E>
Parameters
- $callback : callable(T): U
Tags
Return values
Result<U, E>mapErr()
public
mapErr(callable(E): F $callback) : Result<T, F>
Parameters
- $callback : callable(E): F
Tags
Return values
Result<T, F>mapOr()
public
mapOr(callable(T): U $callback, U $default) : U
Parameters
- $callback : callable(T): U
- $default : U
Tags
Return values
UmapOrElse()
public
mapOrElse(callable(T): U $callback, callable(E): U $default) : U
Parameters
- $callback : callable(T): U
- $default : callable(E): U
Tags
Return values
Umatch()
Applies one of two functions depending on whether the result is Ok or Err.
public
match(callable(T): U $okFn, callable(E): V $errFn) : U|V
Comparable to neverthrow's match method.
Parameters
- $okFn : callable(T): U
-
Function to apply if the Result is Ok
- $errFn : callable(E): V
-
Function to apply if the Result is Err
Tags
Return values
U|V —The result of applying the appropriate function
ok()
public
ok() : Option<string|int, T>
Tags
Return values
Option<string|int, T>or()
public
or(Result<T, F> $right) : Result<T, F>
Parameters
- $right : Result<T, F>
Tags
Return values
Result<T, F>orElse()
public
orElse(callable(E): Result<T, F> $right) : Result<T, F>
Parameters
- $right : callable(E): Result<T, F>
Tags
Return values
Result<T, F>orThrow()
Returns the contained Ok value or throws the provided exception.
public
orThrow(F $exception) : $this
Parameters
- $exception : F
-
The exception to throw if the result is Err
Tags
Return values
$thisunit()
`return` in Haskell. (`Unit` operation.)
public
static unit(TValue $value) : Monad<string|int, TValue>
Parameters
- $value : TValue
Tags
Return values
Monad<string|int, TValue>unwrap()
public
unwrap() : T
Tags
Return values
TunwrapErr()
public
unwrapErr() : E
Tags
Return values
EunwrapOr()
public
unwrapOr(U $default) : T|U
Parameters
- $default : U
Tags
Return values
T|UunwrapOrElse()
public
unwrapOrElse(callable(E): U $default) : T|U
Parameters
- $default : callable(E): U
Tags
Return values
T|UunwrapOrThrow()
Returns the contained Ok value or throws the provided exception.
public
unwrapOrThrow(F $exception) : T
Parameters
- $exception : F
-
The exception to throw if the result is Err