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 的签名吗?