只是想澄清一下。如果我们使用higher-order function
(f. 接受另一个函数作为参数)。"=>"
指定符号来调用它是否有意义by-name
。似乎 arg-functionby-name
无论如何都在调用?
有一个例子:
// 1.
// the function that accepts arg-function with: two int params and returning String
// the function passing v1 & v2 as parameters to arg-function, invoking arg-function 2 times, connecting the result to one string
def takeFunction1(f: (Int, Int) => String, v1:Int, v2:Int ): String = {
f(v1, v2) + f(v1, v2)
}
// 2. same as #1 but calling arg-function by-name
def takeFunction2(f: => ((Int, Int) => String), v1:Int, v2:Int ): String = {
f(v1, v2) + f(v1, v2)
}
def aFun(v1:Int, v2:Int) : String = {
(v1 + v2).toString
}
// --
println( takeFunction1( aFun, 2, 2) )
println( takeFunction2( aFun, 2, 2) )
如果我想这样称呼它怎么办?:
println( takeFunction2( aFun(2,2)), ... ) // it tries to evaluate immediately when passing