-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
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
polymorphic callables #5471
Comments
I found these snippets: https://psalm.dev/r/35c4a4d34e<?php
/**
* @template T
* @var callable(T): T $f
* @param T $t
* @return T
*/
$f = fn($t) => $t;
$f(1);
class Foo
{
/**
* @template A
* @var callable(A): A
*/
private $bar;
/**
* @template A
* @param callable(A): A $bar
*/
public function __construct(callable $bar)
{
$this->bar = $bar;
}
public function baz(): int
{
return ($this->bar)(1);
}
}
|
I don't think the example you provided could be made sound. If you instantiate |
Hack doesn't support templated closures, and I don't think there's much value in Psalm supporting them either. Opens up a whole can of worms. |
|
I found these snippets: https://psalm.dev/r/1d705e3bee<?php
/**
* @template T
*/
class Foo
{
/**
* @var callable(T): T
*/
private $bar;
/**
* @param callable(T): T $bar
*/
public function __construct(callable $bar)
{
$this->bar = $bar;
}
public function baz(): int
{
return ($this->bar)(1);
}
}
|
Thanks for your replies. My snippets actually raised two issues:
data Poly = Poly (forall a. a -> a) The only value with type
/**
* @template T
* @psalm-param T $t
* @return T
*/
function mirror($t) {
return $t;
} works. Still, if I define the same function as an anonymous functions /**
* @template T
* @var callable(T): T $f
* @param T $t
* @return T
*/
$f = fn($t) => $t; the template |
For the latter, you need docblock to immediately precede the closure: https://psalm.dev/r/0f93b31944. Note, however, that Psalm does not support templated closures as @muglug noted above. |
I found these snippets: https://psalm.dev/r/0f93b31944<?php
$_f =
/**
* @template T
* @param T $t
* @return T
*/
fn($t) => $t;
|
I would like to define, accept as parameters and return polymorphic functions like
If I try to provide them with types using parameters, I get errors (see https://psalm.dev/r/35c4a4d34e for some examples). It looks like Psalm is not recognising the
@template
declarationThe text was updated successfully, but these errors were encountered: