This page is no longer maintained — Please continue to the home page at

Re: lazy val

No replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

>>>>> "Luc" == Luc Duponcheel writes:

Luc> FYI: try the following
scala> lazy val foo = { println("evaluating") ; "foo" }
Luc> evaluating foo: java.lang.String = foo

Luc> Note that, when *defining* the lazy value foo, the definition of
Luc> foo *is*evaluated.

This statement is misleading at best. The only reason foo is evaluated
immediately in your example is so the interpreter can print the value.
The entire purpose of "lazy val" is to *not* evaluate the body at
definition time, but wait until the first use.

It would be nice if the interpreter were changed so this didn't happen,
actually. I opened a ticket:

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland