def test1 = { // 1. without ()
println("SS")
}
def test2() = { //2. with ()
println("GG")
}
test1()
第一種寫法,沒有 () 。使用時,不可以加上 () 否則會出錯。因此呼叫時只有一種寫法,test1。
第二種寫法,有 () 。使用時,可以加上 (),也可以不要加上 () 。因此呼叫時有兩種寫法, test2 或 test2()。
def test1 = { // 1. without ()
println("SS")
}
def test2() = { //2. with ()
println("GG")
}
test1()
def repeat(times: Int) (func: => Unit) {
for (n <- 1 to times) func
}
repeat (5) {
println("SSS")
}