Reputation: 1467
I do not understand why this does not compile. It's complaining that friend which is of Mammal type does not have a talk method. But there is...
class Animal {
def talk:String = "i am animal"
}
class Mammal extends Animal {
override def talk:String = "i am mammal"
}
class Cow extends Mammal {
override def talk:String = "moo"
}
trait Farm[+A] {
def set[AA >: A](friend: AA): String
}
class CowFarm extends Farm[Cow] {
override def set[Mammal >: Cow](friend: Mammal): String = friend.talk
}
Upvotes: 0
Views: 72
Reputation: 39654
In set
Mammal
is not the same Mammal
that you declared a few lines above. It's a local generic type parameter that shadowed your existing Mammal
. So your method signature is actually not different to:
override def set[AA >: Cow](friend: AA): String = friend.talk
Since AA
is a supertype of Cow
, it is not guaranteed to have a talk
method. For example, it can be substituted with Any
or AnyRef
.
Upvotes: 4