Scala recursive generics: Parent[Child] and Child[Parent] -


update: clarified , expanded, since original question simplified far

i need pair of traits, each of refers other such parent , child classes must relate each other.

trait parent [c <: child] {   def foo(c: c) }  trait child [p <: parent] {   def parent: p = ...   def bar = parent.foo(this) } 

such implementing classes must come in pairs:

class actualparent extends parent [actualchild] {   def foo(c: actualchild) = ... }  class actualchild extends child [actualparent] { } 

unfortunately, compiler doesn't these traits because generic types aren't complete. instead of c <: child needs c <: child[something]. leaving them unspecified doesn't work either:

trait parent [c <: child[_]] {   def foo(c: c) }  trait child [p <: parent[_]] {   def parent: p = ...   def bar = parent.foo(this) } 

it complains on parent.foo(this) line, because doesn't know this of correct type. type of parent needs parent[this.type] call foo have right types.

i'm thinking there must way of referring object's own type? or type that's required itself?


update: further @daniel's answer, tried using abstract type member within child state generic types of parent type this:

trait parent [c <: child] {   def foo(c: c) }  trait child {   type p <: parent[this.type]    def parent: p = ...   def bar = parent.foo(this) } 

this isn't working when try implement it:

class actualparent extends parent [actualchild] {   def foo(c: actualchild) = ... }  class actualchild extends child {   type p = actualparent } 

gives following error:

overriding type parent in trait child bounds >: nothing <: parent[actualchild.this.type] type parent has incompatible type 

what mean?

you can use approach given in http://programming-scala.labs.oreilly.com/ch13.html:

abstract class parentchildpair {   type c <: child   type p <: parent    trait child {self: c =>     def parent: p   }    trait parent {self: p =>     def child: c   } }  class actualparentchildpair1 {   type c = child1   type p = parent1    class child1 extends child {...}    class parent1 extends parent {...} } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -