Given the following situation (scastie)
trait Element
class Earth extends Element
trait Container[T]
class ContainerEarth extends Container[Earth]
trait Dummy[E <: Element] {
def dummy(container: Container[E]): Any
}
class EarthDummy extends Dummy[Earth] {
def dummy(container: ContainerEarth): Any = ??? // could not override
override def dummy(container: Container[Earth]): Any = ???
}
It's still not clear in my head, why ContaineEarth could not override.
I have a solution to get around the business problem, but I need to understand why does the compiler refuse the substitution.
trait Dummy[E <: Element, C <: Container[E]] {
def dummy(container: C): Any
}
I've been given that variance is in the game, and prevent it.
But for me, variance is about Container[Dirt] being a subtype of Container[Earth] if Dirt extends Earth when Container generic parameter is covariant.
So I'm open to all of your clarifications.