A problem of implicit conversions in scala 2.8
Asked Answered
G

1

8

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).

Geochemistry answered 2/8, 2010 at 16:40 Comment(1)
I think '<:<' parameters are usually called 'ev' (short for 'evidence'), not env.Wenz
T
9

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.

Transliterate answered 2/8, 2010 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.