I did not quite understand @ cyrille-corpet's answer, so I expanded it with a few examples.
class Inv[T] class Variantish[+T, +TVar <: Inv[T]] trait Animal class Dog extends Animal class AnimalInv extends Inv[Animal] class DogInv extends Inv[Dog] val a: Variantish[Animal, Inv[Animal]] = new Variantish[Animal, AnimalInv] val b: Variantish[Animal, Inv[Animal]] = new Variantish[Animal, DogInv] val c: Variantish[Animal, Inv[Animal]] = new Variantish[Dog, AnimalInv] val d: Variantish[Animal, Inv[Animal]] = new Variantish[Dog, DogInv]
a true since Animal <: Animal and AnimalInv <: AnimalInv are true.
b invalid because DogInv <: AnimalInv is false.
c true since Dog <: Animal and AnimalInv <: AnimalInv are true.
d invalid because DogInv <: AnimalInv is false.
Since they show that TVar cannot be covariant.
Even in case d , where the dynamic type is valid, it is not a subtype of the static type.
I suspect that if we look at all the places where we can use TVar in Variantish , then it should not be a type parameter. As @concat noted, any error errors that you can detect can be resolved using an object-safe access modifier.
source share