Is non-local return in Scala new?

Viewed 5450

A colleague just showed me this and I was surprised that it compiled at all:

def toUpper(s: Option[String]): String = {
  s.getOrElse(return "default").toUpperCase
  //          ^^^^^^  // a return here in the closure??
}

and this even works:

println(toUpper(Some("text"))) // TEXT
println(toUpper(None))         // default

I thought return from inside a closure was not allowed. Since when has this worked? Are there caveats with such non-local returns?

2 Answers
Related