一個呼叫按姓名是expensiveOperation()將不會在下面的例子得到運行好處:斯卡拉的call-by-名字 - 聲明參數未使用
呼叫按值:
def test(x: Int, y: Int): Int = x * x
// expensiveOperation is evaluated and the result passed to test()
test(4, expensiveOperation())
的call-by-名稱:
def test(x: Int, y: => Int): Int = x * x
// expensionOperation is not evaluated
test(4, expensiveOperation())
我的問題,雖然是你爲什麼要聲明一個函數參數(在我的情況下,y),當你不打算使用它呢?
接不接'y'作爲參數?你想要什麼 – Jatin
準確!我只是重複Martin Odersky在函數式編程課程中給出的例子 - https://www.coursera.org/course/progfun –