« Return to Thread: Why introduce the lazy keyword?

Re: Why introduce the lazy keyword?

by rinswind-2 :: Rate this Message:

Reply to Author | View in Thread

Florian Hars wrote:

> 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
>

Argh! Just my brain trying to reduce the concept load. Should have tried this experiment myself. 10x :)

 « Return to Thread: Why introduce the lazy keyword?