Unexpected `() => T` behaviour

I am using Scala 2.13.0, on macOS 10.14.6

I’ve created a function for debugging purposes, that takes () => T as a parameter, and behaves in 2 different ways when the parameter is an assigned variable, and when the value is passed without assignment.

It seems that any () => T expression that I would pass straight to my function (without a preceding assignment to variable) will loop forever (meaning that the future never completes).

The function:

import scala.concurrent.{ExecutionContext, Future}; implicit val xc = ExecutionContext.global
def runInBackgroundAndLog[T](block: () => T, waitMessage: String)(implicit xc: ExecutionContext): T = {
  val fut = Future {block.apply}
  var i = 1
  while (!fut.isCompleted) {
    i += 1

Now when I run this:

val f = () => Thread.sleep(6000)
runInBackgroundAndLog(f, "still running")

It prints still running twice, and returns.

But when I run it without a preceding variable assignment, the while loop seems to run forever:

runInBackgroundAndLog(() => Thread.sleep(6000), "still running")

I expect the behaviour of the first example (with preceding variable assignment) to work in both cases, or at least to understand why it is different.

1 Like

Note that it’s a REPL issue. It works outside the REPL, and it also works if you run the REPL with -Yrepl-class-based. This is a very strong clue to the nature of the problem.