Greetings, sir.
Just got a few thoughts about implementing method match
on Option
and Either
.
Like C# functional lib has
This would look like
Option:
$hello = Option::of('Hello');
$result = $hello->match(
some: fn(string $v) => "{$v} world",
none: fn() => 'Hello from none'
);
Either:
/** @var Either<string, FooInterface> $found */
$found = Some::of(new FooImpl());
$found->match(
right: \Fp\id,
left: function(string $errorMessage) {
// sorry for side effect here
$this->logger->logWarning($errorMessage);
return new BarImpl();
},
);
So the method signature would look like:
Option:
/**
* @template TR
* @param callable(A): TR $some
* @param callable(): TR $none
* @return TR
*/
public function match(callable $some, callable $none): mixed
{
Either:
/**
* @template TR
* @param callable(R): TR $right
* @param callable(L): TR $left
* @return TR
*/
public function match(callable $right, callable $left): mixed
{
I think this may be useful.
What do you think?