“return” is a problem if the closure evaluation is deferred or done in another thread. “returns” in closures are implemented by throwing and catching some special exceptions (so they also incur performance cost), so you need to be careful to only invoke a “return” if you are inside the try/ catch block generated by the compiler.
Functional equivalent (or maybe generalization) of returns is continuation passing style. Scala has plugin for delimited continuations: GitHub - scala/scala-continuations: the Scala delimited continuations plugin and library
Continuations are very flexible but they have their own problem when used on JVM. Because JVM doesn’t do tail call optimization, continuations can result in StackOverflowExceptions if used heavily.
By the way, I suspect that result of this expression
is really far from result of this one:
because you probably are messing up Option.getOrElse[B >: A](default: => B): B and Map.getOrElse(key: K, default: => V): V.
Code like Option(32).getOrElse(println, ()) compiles only because several arguments for a single-argument-function is interpreted as a tuple containing all given arguments (in this case, (Unit, Unit)).