【发布时间】:2016-03-02 09:24:31
【问题描述】:
在将函数引用传递给 Scala 中的重载方法时遇到了一个有趣的问题(使用 2.11.7)
以下代码按预期工作
def myFunc(a: Int, b: String): Double = {
a.toDouble + b.toDouble
}
def anotherFunc(value: String, func: (Int, String) => Double) = {
func(111, value)
}
anotherFunc("123123", myFunc)
但是下面的代码不能编译
def myFunc(a: Int, b: String): Double = {
a.toDouble + b.toDouble
}
def anotherFunc(value: String, func: (Int, String) => Double) = {
func(111, value)
}
def anotherFunc(value: Int, func: (Int, String) => Double) = {
func(value, "123123")
}
anotherFunc("123123", myFunc)
编译器喊如下
scala> anotherFunc("123123", myFunc)
<console>:13: error: type mismatch;
found : String("123123")
required: Int
anotherFunc("123123", myFunc)
【问题讨论】:
标签: scala