以下类型是什么:
(Int, => Double) => String
请注意Int
之后的逗号。显然它不是一个句法漏洞,而是与
(Int => Double) => String
E.g。使用重载时:
trait Foo {
def bar(x: (Int, => Double) => String): Unit
def bar(x: (Int => Double) => String): Unit
}
答案 0 :(得分:25)
(Int, => Double) => String
是一个带有名字第二个参数(=> Double
)的函数。
您无法创建Function2[Int, => Double, String]
,但可以创建lambda (Int, => Double) => String
,这意味着相同:
scala> def s:(Int, => Double) => String =
| (a, b) => if (a > 0) a.toString else b.toString
s: (Int, => Double) => String
scala> s(1, {println("test"); 2.0}) //second parameter is not evaluated
res0: String = 1
scala> s(-1, {println("test"); 2.0})
test
res1: String = 2.0