« Return to Thread: Why introduce the lazy keyword?

Re: Why introduce the lazy keyword?

by Florian Hars-3 :: Rate this Message:

Reply to Author | View in Thread

Todor Boev wrote:
> As the title says: why introduce another keyword when there already exists a lazy mechanism for the
> method parameters?
>
> def foo(lazy: => Int)

Because that is not lazy, that is by name:

scala> def f(byName: => Int) = { byName + byName }
f: (=> Int)Int

scala> f({println("Foo"); 2})
Foo
Foo
res20: Int = 4

scala> lazy val g = { println("Bar"); 2 }
Bar
g: Int = 2

scala> g + g
res21: Int = 4

- Florian

 « Return to Thread: Why introduce the lazy keyword?