为什么没有应用这个隐式函数?

时间:2017-08-09 16:48:15

标签: scala implicit-conversion

尝试@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}}纳入范围吗?

0 个答案:

没有答案