psalm
psalm copied to clipboard
Type bounds for generic parameters
How can I fix Bar::foo from this example https://psalm.dev/r/2ec8af4c04 ?
In Scala I can introduce a lower type bound for a new type parameter A1 with lower bound of A: https://scastie.scala-lang.org/3gSkZWZZTnep7lhyzExsZw
More details here https://docs.scala-lang.org/tour/lower-type-bounds.html
I found these snippets:
https://psalm.dev/r/2ec8af4c04
<?php
/**
* @template-covariant A
*/
class Bar
{
/**
* @param A $a
*/
public function foo($a): void
{
}
/**
* @template A1
* @template-bound A1 >: A # A is a lower bound for A1
* @param A1 $a
*/
public function foo2($a): void
{
}
}
Psalm output (using commit 16b24bd):
ERROR: InvalidTemplateParam - 9:15 - Template param A of Bar is marked covariant and cannot be used here