psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Type bounds for generic parameters

Open vkurdin opened this issue 1 year ago • 1 comments

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

vkurdin avatar May 18 '24 21:05 vkurdin

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

psalm-github-bot[bot] avatar May 18 '24 21:05 psalm-github-bot[bot]