Why is this implicit function not being applied?

2019-07-20 05:01发布

问题:

Trying out the implicit conversion of TupleN proposed by @Alexey Romanov in How to apply implicit conversions between tuples?

Given the following implicits:

object ImplicitConversions {

  object A {
    implicit def toA(x: Int): A = A(x)
  }

  case class A(v: Int) extends AnyVal

  implicit def liftImplicitTuple2[A, B, A1, B1](tuple: (A, B))
                                               (implicit f1: A => A1, f2: B => B1): (A1, B1) =
    (f1(tuple._1), f2(tuple._2))
}

and the Tuple (1,2), I can manually apply liftImplicitTuple2 and have it invoke the A implicit conversion:

import ImplicitConversions._

val x = (1, 2)

val y: (A, Int) = liftImplicitTuple2(x)

However, if I just try to have x converted to y, such that:

import ImplicitConversions._

val x = (1, 2)

val y2: (A, Int) = x

I get:

Error:(12, 22) type mismatch;
 found   : (Int, Int)
  required: (sandbox.ImplicitConversions.A, Int)
  val y2: (A, Int) = x

Is there some other import I need to get liftImplicitTuple2 into scope as an implicit?