【发布时间】:2019-06-30 23:37:30
【问题描述】:
疑难解答环境:sbt 控制台 (Scala 2.11.8) & spark-shell (Spark 2.3, Scala 2.11)
我有一个视图绑定类型为 T... 的高阶函数,但是当函数被部分应用时,arg t: T 类型签名从 T <% Double 变为 Nothing。
演示的玩具示例:
// tot: T needs to work on (at least) Int, Long, Double, Float
// no common supertype -> some kind of context bound
def func[T <% Double](isValid: Boolean)(tot: T, cnt: Int): Double =
if (isValid) tot.toDouble / cnt else Double.NaN
当我尝试部分应用 isValid 时,我希望结果为 (T, Int) => Double 类型,但类型最终为 (Nothing, Int) => Double,我无法传入 arg tot。
val f1 = func(true)_ // f1: (Nothing, Int) => Double = <function2>
val f2 = func(false)_ // f2: (Nothing, Int) => Double = <function2>
val g1 = f1(10.0, 1)
// <console>:40: error: type mismatch;
// found : Double(10.0)
// required: Nothing
// val g1 = f1(10.0, 1)
我在定义 f1 或 f2 时没有收到任何错误消息...所以很难解释。它只是将 arg tot: T 转换为类型 Nothing。
检查 scala 文档...我看到 scala.Nothing 是所有其他类型的子类型,所以我认为它可能正在丢失 T 上的视图绑定...这可能与类型擦除有关...所以我尝试了使用 ClassTag...
import scala.reflect.ClassTag
def func[T <% Double](isValid: Boolean)(tot: T, cnt: Int)(implicit tag: ClassTag[T]): Double =
if (isValid) tot.toDouble / cnt else Double.NaN
这没有帮助。同样的问题。
如果我尝试使用implicit num: Numeric[T],它会以一种新的方式阻塞Nothing 类型...
def func[T](isValid: Boolean)(tot: T, cnt: Int)( implicit num: Numeric[T] ): Double =
if (isValid) num.toDouble(tot) / cnt else Double.NaN
val f1 = func(true)_
// <console>:40: error: could not find implicit value for parameter num: Numeric[Nothing]
// val f1 = func(true)_
如果我一次全部应用它(在顶部使用第一个 'func'),它可以正常工作...
val g1 = func(true)(10.0, 1)
// g1: Double = 10.0
但在我的真实(非玩具)代码中,这不是一个选项。
这里发生了什么,我怎样才能使 func 在部分应用时工作?
编辑 [@Alexey 的解决方案]
我无法使用首选的“def”方法。
def func[T <% Double](isValid: Boolean)(tot: T, cnt: Int): Double =
if (isValid) tot.toDouble / cnt else Double.NaN
// func: [T](isValid: Boolean)(tot: T, cnt: Int)(implicit evidence$1: T => Double)Double
def f1[T <% Double]: ((T, Int) => Double) = func[T](true)_
// f1: [T](implicit evidence$1: T => Double)(T, Int) => Double
f1[Double](10.0, 1)
<console>:41: error: too many arguments for method f1: (implicit evidence$1: Double => Double)(Double, Int) => Double
f1[Double](10.0, 1)
【问题讨论】:
标签: scala types polymorphism higher-order-functions