Why is this implicit function not being applied?

蹲街弑〆低调 提交于 2020-01-04 09:13:54

问题


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?

来源:https://stackoverflow.com/questions/45596456/why-is-this-implicit-function-not-being-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!