I want to write a implicit conversion of Tuple2[A,B] to Seq[C] where C is super type of both A and B. My first try as following:
implicit def t2seq[A,B,C](t: (A,B))(implicit env: (A,B) <:< (C,C)): Seq[C] = {
val (a,b) = env(t)
Seq(a,b)
}
But it doesn't work:
scala> (1,2): Seq[Int]
<console>:7: error: type mismatch;
found : (Int, Int)
required: Seq[Int]
(1,2): Seq[Int]
^
While this one works:
class Tuple2W[A,B](t: (A,B)) {
def toSeq[C](implicit env: (A,B) <:< (C,C)): Seq[C] = {
val (a,b) = env(t)
Seq(a,b)
}
}
implicit def t2tw[A,B](t: (A,B)): Tuple2W[A,B] = new Tuple2W(t)
Use case:
scala> (1,2).toSeq
res0: Seq[Int] = List(1, 2)
I have no idea why the first solution didn't work as expected. Scala version 2.8.0.r22634-b20100728020027 (Java HotSpot(TM) Client VM, Java 1.6.0_20).