The wikipedia article for Scala even answers what the lazy
keyword does:
Using the keyword lazy defers the initialization of a value until this value is used.
Additionally, what you have in this code sample with q : => Parser[U]
is a call-by-name parameter. A parameter declared this way remains unevaluated, until you explicitly evaluate it somewhere in your method.
Here is an example from the scala REPL on how the call-by-name parameters work:
scala> def f(p: => Int, eval : Boolean) = if (eval) println(p)
f: (p: => Int, eval: Boolean)Unit
scala> f(3, true)
3
scala> f(3/0, false)
scala> f(3/0, true)
java.lang.ArithmeticException: / by zero
at $anonfun$1.apply$mcI$sp(<console>:9)
...
As you can see, the 3/0
does not get evaluated at all in the second call. Combining the lazy value with a call-by-name parameter like above results in the following meaning: the parameter q
is not evaluated immediately when calling the method. Instead it is assigned to the lazy value p
, which is also not evaluated immediately. Only lateron, when p
is used this leads to the evaluation of q
. But, as p
is a val
the parameter q
will only be evaluated once and the result is stored in p
for later reuse in the loop.
You can easily see in the repl, that the multiple evaluation can happen otherwise:
scala> def g(p: => Int) = println(p + p)
g: (p: => Int)Unit
scala> def calc = { println("evaluating") ; 10 }
calc: Int
scala> g(calc)
evaluating
evaluating
20
scala lazy
.scala lazy
or evenscala lazy argument
doesn't seem to yield a lot of useful info because you'll mostly get results about the more basic stuff like lazy val's and call-by-name.lazy val
on the same line. I thought you were merely asking what alazy val
is.