fp4php/functional

Change return types of `Either::right` and `Either::left`

klimick opened this issue · 0 comments

Like in #15.
Either::left and Either::right currently returns Left<LI, empty> and Right<empty, RI>.

For this snippet psalm infers weird type for the $result Either<string, Left<"less than 10", empty> | float | int>
But if we change return types of Either::right and Either::left inferred type will be correct: Either<string, int>

/**
 * @return Either<string, int>
 */
function getNum(): Either
{
    return Either::right(10);
}

/** @psalm-trace $result */
$result = Either::do(function() {
    $num1 = yield getNum();
    $num2 = yield Either::right(20);

    if ($num1 < 10) {
        return yield Either::left('less than 10');
    }

    return $num1 + $num2;
});