Update : Clarified and expanded since the original question was too simplified.
I need a couple of signs, each of which refers to another, so the parent and child classes must relate to each other.
trait Parent [C <: Child] { def foo(c: C) } trait Child [P <: Parent] { def parent: P = ... def bar = parent.foo(this) }
So that implementing classes come in pairs:
class ActualParent extends Parent [ActualChild] { def foo(c: ActualChild) = ... } class ActualChild extends Child [ActualParent] { }
Unfortunately, the compiler does not like these traits because generic types are not complete. Instead of C <: Child you need to say C <: Child[ something ] . Leaving them unspecified also does not work:
trait Parent [C <: Child[_]] { def foo(c: C) } trait Child [P <: Parent[_]] { def parent: P = ... def bar = parent.foo(this) }
Now he complains about the string parent.foo(this) because he does not know that this is of the correct type. The parent type must be Parent[this.type] to call foo in order to have the required types.
I think there should be a way to refer to the native type of an object? Or to the type that should be yourself?
Refresh . In response to @Daniel, I tried using an abstract type member in a child language to specify generic types of the parent type as follows:
trait Parent [C <: Child] { def foo(c: C) } trait Child { type P <: Parent[this.type] def parent: P = ... def bar = parent.foo(this) }
This does not work when I try to implement it:
class ActualParent extends Parent [ActualChild] { def foo(c: ActualChild) = ... } class ActualChild extends Child { type P = ActualParent }
Gives the following error:
overriding type Parent in trait Child with bounds >: Nothing <: Parent[ActualChild.this.type] type Parent has incompatible type
What does it mean?