Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If the boolean operators &&, || are implemented as infix methods, then isn't short-circuit evaluation just a consequence of being implemented using call-by-name semantics, since, for example, the && method wouldn't then evaluate the second arg in any case if the first is false.

I'm just wondering because they are generally treated as separate topics (e.g in the Scala intro course I'm taking - ) but short-circuit evaluation seems to just be a consequence of call-by-name.

share|improve this question
up vote 1 down vote accepted

Turns out Martin answers the exact question 15 minutes later on in the same video! Yes, short-circuit evaluation is implemented using call by-name args. Martin discusses just this and defines the and function as follows:

  def and(x:Boolean, y: => Boolean) {
    if (x) y else false

Note that if the second arg y was not call-by-name, then and(false,<infinite loop expr>) would not terminate, whereas we would expect it to return false due to short-circuit eval.

share|improve this answer

I wouldn't say that it's a consequence. It is however a requirement.

share|improve this answer
It is a good thing someone posted an actual answer; yours is just an opinion. Imagine someone posting the opposite view, with just as little argumentation; which person would be correct? – Martijn Pieters Dec 11 '13 at 9:04
It's not an opinion, it's a fact. Why? Well because you can implement non-short-circuit evaluation with call-by-name semantics, but you cannot implement short-circuit evaluation without it. Hence that call-by-name semantics are a requirement. It is however not a consequence, since you still have your options open if call-by-name is used. This argumentation at least holds if you equate call-by-name with lazy evalutation, which I believe that the OP was doing. – Alexander Torstling Dec 11 '13 at 9:53
Then put that in your answer. Explain why. Don't just state it is so. – Martijn Pieters Dec 11 '13 at 9:54
@MartijnPieters fair enough. I suppose I could be a bit more clear. Wrote the answer on my cell, so was a bit too short I suppose. – Alexander Torstling Dec 11 '13 at 9:56
I commented because your post showed up in the low quality review queue (already downvoted by that time), which shows that the system thought it was a bit too short. :-) – Martijn Pieters Dec 11 '13 at 9:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.