Reputation: 3390
class Animal
class Cat extends Animal
trait ZPar {
type K
def get(i: K)
}
def zz(t:ZPar{ type K >: Animal } ) = {
t.get(new Cat) //compiled! why?
}
This code compiles without errors, but I assumed that there must be an issue because K >: Animal
and I'm passing Cat
which is definitely not super type of Animal
.
Is it something wrong with compiler or I just misunderstood the concept?
Upvotes: 0
Views: 40
Reputation: 51683
It's not necessary that new Cat
is of type Cat.new Cat
can be not only of type Cat
. It can be of types Cat
, Animal
, AnyRef
or Any
.
So here K
is inferred to be minimal possible type, i.e. Animal
.
Upvotes: 1
Reputation: 170805
An argument of t.get
must have type t.K
. The compiler doesn't know exactly what type that is, but it does know it's a supertype of Animal
and therefore of Cat
. So any value of type Cat
has type t.K
as well.
Upvotes: 1