我做了一个单元测试来研究 Scala 函数文字格式,发现它很混乱,你能帮我理解不同语法的含义吗?
@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
提前致谢。