generics - Scala nested case class self-bounding inheritance -
i'm working on a* implementation in scala. accomplish clean structure use nested case class structure implements self-bounded trait. however, experience issues when implementing in scala ide. following code not compile:
trait a[t <: a[t]] class b { case class c(int: int) extends a[c] // a[this.c] won't work either def make = c(5) } object d { def foo[t <: a[t]](some: t) = {} val c = new b().make foo(c) // not compile } is there way can make structure work?
not sure why want this, here's why won't work is:
the type of d.c b#c. path-dependent type don't know instance of b belongs to. however, c extends a[c], same saying a[this.c] in context, bound specific instance of b. foo sees type parameter t b#c, not same b.c b.
you have 2 options make compile.
relax constraints of a b#c:
trait a[t <: a[t]] class b { case class c(int: int) extends a[b#c] def make = c(5) } object d { def foo[t <: a[t]](some: a[t]) = {} val c = new b().make foo(c) } or handle path-dependent type, c has type b.c:
trait a[t <: a[t]] class b { case class c(int: int) extends a[c] def make = c(5) } object d { def foo[t <: a[t]](some: a[t]) = {} val b = new b val c: b.c = b.make foo(c) }
Comments
Post a Comment