You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can call the templated function identity, and expect it to return exactly the provided type.
However, the makeIdentity function will not work as expected, unless I manually specify the templated type of the returned callable using a @var annotation.
Of course, I can't use the returned callable and expect it to work as the identity function, precisely because I set a specific template type when creating the function (if I hadn't provided any type, psalm would've defaulted to empty).
What I think would be really neat to have is a way to express this inside of psalm, either as:
a @template-return annotation that is related to a returned callable
a @psalm-infer-return to allow returning exactly the inferred type (aka exactly the templated function declared earlier; this is also extremely useful in some other scenarios).
The text was updated successfully, but these errors were encountered:
<?php/** * @template A * @param A $a * @return A */functionidentity($a) { return$a; }
/** * @template A * @return (callable(A): A) */functionmakeIdentity(): callable { return'identity'; }
/** @psalm-trace $a */$a = identity('a');
/** @psalm-trace $identity */$identity = makeIdentity();
/** @psalm-trace $b */$b = $identity('b');
Psalm output (using commit 3ce41d7):
INFO: Trace - 16:1 - $a: "a"
INFO: Trace - 19:1 - $identity: callable(empty):empty
ERROR: InvalidScalarArgument - 22:16 - Argument 1 expects empty, "b" provided
INFO: Trace - 22:1 - $b: empty
INFO: UnusedVariable - 16:1 - $a is never referenced or the value is not used
INFO: UnusedVariable - 22:1 - $b is never referenced or the value is not used
Currently, there is no way in psalm to express a function that returns a callable, if the callable has standalone template arguments.
For example: https://psalm.dev/r/7be6130bcb
I can call the templated function
identity
, and expect it to return exactly the provided type.However, the
makeIdentity
function will not work as expected, unless I manually specify the templated type of the returned callable using a@var
annotation.Of course, I can't use the returned callable and expect it to work as the
identity
function, precisely because I set a specific template type when creating the function (if I hadn't provided any type, psalm would've defaulted toempty
).What I think would be really neat to have is a way to express this inside of psalm, either as:
@template-return
annotation that is related to a returned callable@psalm-infer-return
to allow returning exactly the inferred type (aka exactly the templated function declared earlier; this is also extremely useful in some other scenarios).The text was updated successfully, but these errors were encountered: