Reputation: 838
I have a code like this
trait Toy
trait Child {
type T <: Toy
def toys : Seq[T]
def play(toys: Seq[T]): Unit
}
trait Parent {
type C <: Child
def firstChild: C
}
trait Home {
def parent: Parent
def toys: Seq[Parent#C#T]
def apply() = {
val ts = toys
parent.firstChild.play(toys)
}
}
but I can't compile it:
[error] .../module/common/src/main/scala/test/Debug.scala:21:32: type mismatch;
[error] found : Seq[test.Parent#C#T]
[error] required: Seq[_12.T] where val _12: test.Parent#C
[error] parent.firstChild.play(toys)
Is there any way to fix this error without converting all abstract-types to param-types?
Upvotes: 0
Views: 136
Reputation: 10764
One way to make it compile is:
trait Toy
trait Child {
type T <: Toy
def toys : Seq[T]
def play(toys: Seq[T]): Unit
}
trait Parent { parent =>
type C <: Child
val firstChild: C
}
trait Home {
val parent: Parent
def toys: Seq[parent.firstChild.T]
def apply() = {
val ts = toys
parent.firstChild.play(toys)
}
}
Remember that type members are bound to their outer instances. If you give a toy to a child, it must be a toy meant specifically for that particular child of that particular parent (parent.firstChild.T
), not any potential child of any potential parent (Parent#C#T
).
Upvotes: 2