We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I'm trying to implement a kind of "auto-boxing" of generic result objects. If a non-result value gets passed, it is automatically wrapped:
/** * @template TNextValue * @template TNextError * * @param Result<TNextValue, TNextError>|TNextValue $result * @return Result<TValue|TNextValue, TNextError> */ abstract function orElse($result): Result;
However, I'm getting a wrong return type when I actually call this method:
https://psalm.dev/r/40be01250b
It seems to work fine in PHPStan: https://phpstan.org/r/c9779525-79a6-4a8f-b5f5-9d1c652c204f
I don't think the |TNextValue part should be considered in this case, because Result<TNextValue, TNextError> is more specific.
|TNextValue
Result<TNextValue, TNextError>
For completness, my actual code involves closures (and exhibits the same issue):
https://psalm.dev/r/9880a39426
Also works in PHPStan: https://phpstan.org/r/cd2248d1-2862-477d-ba0a-65ab69445f25
The text was updated successfully, but these errors were encountered:
I found these snippets:
<?php declare(strict_types=1); /** * @template-covariant TValue * @template-covariant TError * @psalm-suppress InvalidTemplateParam not relevant */ abstract class Result { /** * @template TNextValue * @template TNextError * * @param Result<TNextValue, TNextError>|TNextValue $result * @return Result<TValue|TNextValue, TNextError> */ abstract function orElse($result): Result; } /** * @param Result<int, string> $result * @param Result<never, float> $elseResult * @psalm-suppress UnusedVariable not relevant */ function example(Result $result, Result $elseResult): void { /** * @psalm-trace $x * @psalm-check-type $x = Result<int, float> */ $x = $result->orElse($elseResult); }
Psalm output (using commit 765dcbf): INFO: Trace - 31:5 - $x: Result<Result<never, float>|int, float> ERROR: CheckType - 31:5 - Checked variable $x = Result<int, float> does not match $x = Result<Result<never, float>|int, float>
<?php declare(strict_types=1); /** * @template-covariant TValue * @template-covariant TError * @psalm-suppress InvalidTemplateParam not relevant */ abstract class Result { /** * @template TNextValue * @template TNextError * * @param Closure(TError, mixed...):(Result<TNextValue, TNextError>|TNextValue) $callback * @return Result<TValue|TNextValue, TNextError> */ abstract function orElse(\Closure $callback): Result; } /** * @param Result<int, string> $result * @param Closure():Result<never, float> $callback * @psalm-suppress UnusedVariable not relevant */ function example(Result $result, Closure $callback): void { /** * @psalm-trace $x * @psalm-check-type $x = Result<int, float> */ $x = $result->orElse($callback); }
Sorry, something went wrong.
No branches or pull requests
I'm trying to implement a kind of "auto-boxing" of generic result objects. If a non-result value gets passed, it is automatically wrapped:
However, I'm getting a wrong return type when I actually call this method:
https://psalm.dev/r/40be01250b
It seems to work fine in PHPStan: https://phpstan.org/r/c9779525-79a6-4a8f-b5f5-9d1c652c204f
I don't think the
|TNextValue
part should be considered in this case, becauseResult<TNextValue, TNextError>
is more specific.For completness, my actual code involves closures (and exhibits the same issue):
https://psalm.dev/r/9880a39426
Also works in PHPStan: https://phpstan.org/r/cd2248d1-2862-477d-ba0a-65ab69445f25
The text was updated successfully, but these errors were encountered: