Accessing type-parameter of a type-parameter

Posted by itemState on Stack Overflow See other posts from Stack Overflow or by itemState
Published on 2011-01-03T15:57:59Z Indexed on 2011/01/04 1:53 UTC
Read the original article Hit count: 537

Filed under:
|

i would like to access, in a trait, the type-parameter of a type-parameter of that trait. without adding this "second-order" type-parameter as another "first-order" parameter to the trait. the following illustrates this problem:

sealed trait A; sealed trait A1 extends A; sealed trait A2 extends A
trait B[ ASpecific <: A ] { type ASpec = ASpecific }
trait D[ ASpecific <: A ] extends B[ ASpecific ]
trait C[ +BSpecific <: B[ _ <: A ]] {
   def unaryOp : C[ D[ BSpecific#ASpec ]]
}

def test( c: C[ B[ A1 ]]) : C[ D[ A1 ]] = c.unaryOp

the test fails to compile because apparently, the c.unaryOp has a result of type C[D[A]] and not C[D[A1]], indicating that ASpec is merely a shortcut for _ <: A and does not refer to the specific type parameter.

the two-type-parameter solution is simple:

sealed trait A; sealed trait A1 extends A; sealed trait A2 extends A
trait B[ ASpecific <: A ]
trait D[ ASpecific <: A ] extends B[ ASpecific ]
trait C[ ASpecific <: A, +BSpecific <: B[ ASpecific ]] {
   def unaryOp : C[ ASpecific, D[ ASpecific ]]
}

def test( c: C[ A1, B[ A1 ]]) : C[ A1, D[ A1 ]] = c.unaryOp

but i don't understand why i need to clutter my source with this second, obviously redundant, parameter. is there no way to retrieve it from trait B?

© Stack Overflow or respective owner

Related posts about scala

Related posts about type-parameter