【发布时间】:2021-04-06 03:09:45
【问题描述】:
以下示例中有两个类似的无形多态函数。它们之间的唯一区别是deserSucceeding 的隐式案例定义有一个额外的子类型证据(implicit e: FS <: FromStr[T])。 Scala 无法为deserFailing 导出隐式Aux.Case,但为deserSucceeding 成功。
为什么?这是 scala 编译器的限制还是 deserSucceeding 会导致隐式推导/类型推断中的歧义?
import shapeless._
type FromStr[T] = String => T
object FromDouble extends FromStr[Double] {
override def apply(v1: String): Double = v1.toDouble
}
object deserFailing extends Poly2 {
implicit def kase[T, FS <: FromStr[T]]: Case.Aux[String, FS, T] = at((s, fs) => fs(s))
}
// fails to derive a `Case.Aux`
deserFailing("1.0", FromDouble)
object deserSucceeding extends Poly2 {
implicit def kase[T, FS](implicit e: FS <:< FromStr[T]): Case.Aux[String, FS, T] = at((s, fs) => fs(s))
}
deserSucceeding("1.0", FromDouble)
【问题讨论】:
标签: scala type-inference implicit generic-programming shapeless