By-name parameters

def calculate(input: => Int) = input * 37
def whileLoop(condition: => Boolean)(body: => Unit): Unit =
  if (condition) {
    body
    whileLoop(condition)(body)
  }

var i = 2

whileLoop (i > 0) {
  println(i)
  i -= 1
}  // prints 2 1
  • Are evaluated every time they are used.
  • They won’t be evaluated at all if they are unused.
  • This is similar to replacing the by-name parameters with the passed expressions.

By-name parameters — Structure map

Clickable & Draggable!

By-name parameters — Related pages: