按名称调用的好处之一是在以下示例中不会运行昂贵的操作():
按值调用:
def test( x: Int, y: Int ): Int = x * x
// expensiveOperation is evaluated and the result passed to test()
test( 4, expensiveOperation() )
直呼:
def test( x: Int, y: => Int ): Int = x * x
// expensionOperation is not evaluated
test( 4, expensiveOperation() )
我的问题是,当你不打算使用函数参数时,为什么要声明一个函数参数(在我的例子中是 y)?