Я сделал единичный тест для изучения текстового формата функции Scala и нашел его довольно запутанным, не могли бы вы помочь мне понять смысл различного синтаксиса?scala function literal confusion
@Test def supplierLiteral: Unit = {
object Taker {
def takeFunctionLiteral(supplier: => Int): Unit = {
println("taker takes")
// println(supplier.apply()) //can't compile
println(supplier)
}
def takeExplicitFunction0(supplier:() => Int): Unit = {
println("taker takes")
println(supplier())
}
}
val give5:() => Int =() => {
println("giver gives")
5
}
println(give5.isInstanceOf[Function0[_]])
Taker.takeFunctionLiteral(give5) //can't compile, expected Int
println()
Taker.takeExplicitFunction0(give5)
}
Почему println(suppiler.apply())
неправильный синтаксис в takeFunctionLiteral
? Не оба эквивалента? В чем разница между
supplier:() => Int
и
supplier: => Int
Спасибо заранее.
См. Http://stackoverflow.com/questions/32802104/some-questions-about-difference-between-call-by-name-and-0-arity-functions – Yawar