scala. função de ordem superior chamando pelo nome. isso faz sentido
Só quero esclarecer. Se nós usarmoshigher-order function
(f. que aceita outra função como argumento). Faz algum sentido especificar"=>"
assine para chamá-loby-name
. Parece arg-função está chamandoby-name
de qualquer maneira?
Existe um exemplo:
// 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) )
E se eu quiser chamá-lo assim:
println( takeFunction2( aFun(2,2)), ... ) // it tries to evaluate immediately when passing