获得SAM' d的隐含论据?

时间:2016-12-11 12:20:22

标签: scala scala-2.12

这里有一些代码在2.11中编译,但在2.12中失败:

object Main extends App {
  implicit val c: Configured[String] = new Configured[String] { def apply(v: CfgValue): Option[String] = None }
  val a = Configured[String]
}

sealed trait CfgValue {
  def convertTo[A:Configured]: Option[A] = implicitly[Configured[A]].apply(this)
}
trait Configured[A] {
  def apply(v: CfgValue): Option[A]
}

object Configured {
  def apply[A:Configured]: Configured[A] = implicitly[Configured[A]]

  def apply[A](f: CfgValue => Option[A]): Configured[A] = new Configured[A] {
    def apply(v: CfgValue) = f(v)
  }
}

在2.12中失败:

[error] /tmp/rendererKzypu6fJSu/src/main/scala/test.scala:7: ambiguous reference to overloaded definition,
[error] both method apply in object Configured of type (f: CfgValue => Option[String])Configured[String]
[error] and  method apply in object Configured of type (implicit evidence$2: Configured[String])Configured[String]
[error] match expected type ?
[error]   val a = Configured[String]
[error]   

在隐式扩展等之后,两个方法签名是

def apply[A](f: CfgValue => Option[A]): Configured[A]
def apply[A](implicit f: Configured[A]): Configured[A]

在SAM之后,它们是:

def apply[A](f: CfgValue => Option[A]): Configured[A]
def apply[A](implicit f: CfgValue => Option[A]): Configured[A]

任何保留2.12签名的方法?

0 个答案:

没有答案