Is a while loop already implemented with pass-by-name parameters? : Scala

568 views Asked by At

The Scala Tour Of Scala docs explain pass-by-name parameters using a whileLoop function as an example.

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

The section explains that if the condition is not met then the body is not evaluated, thus improving performance by not evaluating a body of code that isn't being used.

Does Scala's implementation of while already use pass-by-name parameters?

If there's a reason or specific cases where it's not possible for the implementation to use pass-by-name parameters, please explain to me, I haven't been able to find any information on it so far.

EDIT: As per Valy Dia's (https://stackoverflow.com/users/5826349/valy-dia) answer, I would like to add another question...

Would a method implementation of the while statement perform better than the statement itself if it's possible not to evaluate the body at all for certain cases? If so, why use the while statement at all?

I will try to test this, but I'm new to Scala so it might take me some time. If someone would like to explain, that would be great.

Cheers!

2

There are 2 answers

1
Thilo On BEST ANSWER

Would a method implementation of the while statement perform better than the statement itself if it's possible not to evaluate the body at all for certain cases?

No. The built-in while also does not evaluate the body at all unless it has to, and it is going to compile to much more efficient code (because it does not need to introduce the "thunks"/closures/lambdas/anonymous functions that are used to implement "pass-by-name" under the hood).

The example in the book was just showing how you could implement it with functions if there was no built-in while statement.

I assumed that they were also inferring that the while statement's body will be evaluated whether or not the condition was met

No, that would make the built-in while totally useless. That is not what they were driving at. They wanted to say that you can do this kind of thing with "call-by-name" (as opposed to "call-by-value", not as opposed to what the while loop does -- because the latter also works like that).

The main takeaway is that you can build something that looks like a control structure in Scala, because you have syntactic sugar like "call-by-name" and "last argument group taking a function can be called with a block".

1
Valy Dia On

The while statement is not a method, so the terminology by-name parameter is not really relevant... Having said so the while statement has the following structure:

while(condition){
  body
}

where the condition is repeatedly evaluated and the body is evaluated only upon the condition being verified, as show this small examples:

scala> while(false){ throw new Exception("Boom") }
// Does nothing

scala> while(true){ throw new Exception("Boom") }
// java.lang.Exception: Boom

scala> while(throw new Exception("boom")){ println("hello") }
// java.lang.Exception: Boom