Scala's lazy arguments: How do they work?

Posted by python dude on Stack Overflow See other posts from Stack Overflow or by python dude
Published on 2012-03-21T16:59:16Z Indexed on 2012/03/21 17:29 UTC
Read the original article Hit count: 222

Filed under:
|

In the file Parsers.scala (Scala 2.9.1) from the parser combinators library I seem to have come across a lesser known Scala feature called "lazy arguments". Here's an example:

def ~ [U](q: => Parser[U]): Parser[~[T, U]] = { lazy val p = q // lazy argument
  (for(a <- this; b <- p) yield new ~(a,b)).named("~")
}

Apparently, there's something going on here with the assignment of the call-by-name argument q to the lazy val p.

So far I have not been able to work out what this does and why it's useful. Can anyone help?

© Stack Overflow or respective owner

Related posts about scala

Related posts about lazy