; last updated - 4 minutes read

When I wrote my article about parameter passing mechanisms, I believed the Call by Name to be an old fashioned idea. However, it can be pretty useful if you know what you are doing, so the Scala language (collecting clever ideas like a vacuum cleaner :)) knows it as well.

The cool thing about Call by Name parameter is that they are evaluated when they are used.

This is to say: If they aren't used, they aren't evaluated at all.

This comes in handy if there is a parameter that is expensive to evaluate but is needed only in rare cases. A typical example is logging debug messages, as Arno Haase describes in his Java Magazin article. Most of the time, the debug flag is deactivated in a production environment, so it would be a waste of time to evaluate the expression that is not logged. Java programmer usually solve this problem like this:

if (log.isDebugEnabled()) { log.debug("Function result = " + veryComplicatedMathFunction()); }

I'm tired of writing the identical if statement ever and ever again. Wouldn't it be nice to write a method logDebugMessages(String message) that contains the if statement?

You can do so in Java. Unfortunately, the parameter is always evaluated, so in production mode this solution is slower than the verbose one.

Scala comes to the rescue. It allows you to write a function without performance penalty by using something that resembles a closure:

def logDebugMessage(message: => String) { if (log.isDebugEnabled()) { log.debug(message); } }

You convert any parameter into a Call by Name parameter by simply inserting an arrow between the colon and the type definition.

There is another interesting example: You can introduce Java's ternary operator[1] by defining a method called ?. (This is not a typo, the name of the method is just a question mark). As you can read here, named parameters allow you to define the following method

def ?[T](test: => Boolean)(body: => T)(elsebody: => T) = { if (test) body else elsebody }

and to use it like this:

// print x and decrement it if it's a positive number println ((x<=0) ? (0) (x--))

In this particular case you don't want the second parameter to be executed if x is already smaller of equal to zero. This can't be achieved without Call by Name parameters.

By the way, this example shows the astonishing power of Scala's type inference. The ternary operator method uses a generic type parameter that can be inferred automatically by the compiler from the terms "(0)" and "(x--)".

... can hurt...

If you use this parameter passing mechanism, beware of its caveats. As I mentioned before, the expression can be evaluated once, twice, many times. Maybe it's evaluated not at all. You just don't know. So you have to be aware of this. Arno Haase describes a useful application of this feature - you can rewrite the while statement as a function in Scala - but in most cases it's a dangerous toy. Consider our debug logging example:

val number: Integer = 0; logDebugMessage("The number is " + number++);

The catch is that the number is incremented in debug mode but remains unchanged in production mode. The programmer using the logDebugMessage function can't rely on the number to be incremented. Of course, he[2] can easily find out whether he can rely on it by looking at at the signature of the method he calls. But then, it's just a small arrow that can easily be overlooked, and be honest - how often do you look thoroughly at a signature of a method you've already been using a couple of times?

... but still, it's good to have a sharp knife!

Well, you have to be aware of the problem, and try to solve it one way or another. I guess every team has to find its own solution. I guess many teams will be happy be making the parameter passing mechanism transparent by using naming conventions. That said, Call by Name is a very interesting parameter passing mechanism that has many advantages if used wisely and I'm glad to learn it's part of a modern programming language like Scala.

  • Java Magazin 06/2012, Scala Bytes: Sugartime, by Arno Haase
  • http://www.naildrivin5.com/scalatour/wiki_pages/CallByName

  1. Scala doesn't need dedicated ternary operator because the if statement does the same trick if you use it in an expression. But still, it's an interesting example.
  2. or she. Please apologize that I'll drop the second personal pronoun in order to keep my sentences simple.