<U, E is Throwable>
Execute a callable and transform the result into an Option
as Option\of()
does
but also return Option\None
if it an exception matching $exceptionClass was thrown.
Successful execution:
self::assertEq(Option\tryOf(fn () => strtolower("FRUITS")), Option\some("fruits"));
Convertion of null
to Option\None
:
self::assertEq(Option\tryOf(fn() => null), Option\none());
Checked Exception:
self::assertEq(Option\tryOf(fn () => new \DateTimeImmutable("nope")), Option\none());
Unchecked Exception:
self::assertEq(Option\tryOf(fn () => 1 / 0), Option\none());
// @throws DivisionByZeroError Division by zero
Parameters | |||
---|---|---|---|
callable(): U
|
$callback |
# | |
mixed
|
$noneValue = null |
# | |
bool
|
$strict = true |
# | |
class-string<E>
|
$exceptionClass = Exception::class |
# |
Returns | |
---|---|
Option<U>
|
Throws | |
---|---|
Throwable |