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).
You only need to use <:<
if the parameters to be restricted are already bound in the surrounding scope (as they are in your second try), so in your case
implicit def t2seq[A <: C,B <: C,C](t: (A,B)) = Seq(t._1, t._2)
is sufficient.
I would guess that your first try did not work as it is too complex for the type inferencer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With