Call by name is lazy.
def maybeTwice(b: Boolean, i: => Int) = {
if(b) 2 * i else 0
}
maybeTwice(true, {
// the block is run
println("run!")
3 + 1
})
maybeTwice(false, {
// this block is not run
println("run!")
3 + 1
})
0 comments:
Post a Comment