This doesn't unpack the types, but it does constrain the types A
and B
when calling get
.
trait Container {
type Element
}
trait AssociativeContainer extends Container {
type Element <: Tuple2[_, _]
def get[A, B](x: A)(implicit ev: (A, B) =:= Element): B
}
This looks promising, but is cheating -- it doesn't work if Element
is abstract.
def Unpack[T<:Tuple2[_, _]] = new {
def apply[A, B](implicit ev: T <:< (A, B)) = new {
type AA = A
type BB = B
}
}
trait AssociativeContainer {
type Element = (Int, String)
val unpacked = Unpack[Element].apply
type A = unpacked.AA
type B = unpacked.BB
1: A
"": B
def get(x: A): B
}