尝试@Alexey Romanov在How to apply implicit conversions between tuples?提出的TupleN的隐式转换
鉴于以下含义:
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))
}
和Tuple
(1,2),我可以手动应用liftImplicitTuple2
并让它调用A
隐式转换:
import ImplicitConversions._
val x = (1, 2)
val y: (A, Int) = liftImplicitTuple2(x)
但是,如果我只是尝试将x
转换为y
,那么:
import ImplicitConversions._
val x = (1, 2)
val y2: (A, Int) = x
我明白了:
Error:(12, 22) type mismatch;
found : (Int, Int)
required: (sandbox.ImplicitConversions.A, Int)
val y2: (A, Int) = x
我需要一些其他导入才能将liftImplicitTuple2
作为隐式的<{1}}纳入范围吗?