I'm attempting to perform some retry logic with Scala. For the sake of providing a MRE, I have replaced the exception-throwing object with Y. When running X.main. I expect to see this print two failure messages, then one success message, but it appears that the return statement is effectively acting as a no-op, and I'm seeing 3 success messages.
I realize this isn't idiomatic Scala and there are plenty of other ways to accomplish this, but is there no way to return from within a for loop in Scala? If not, why is that?
object X {
def main(args : Array[String]) : Unit = {
val y = new Y()
for ( i <- 1 to 5 ) {
try {
y.doSomething()
return
} catch {
case e : Throwable =>
if ( i == 5 ) {
throw e
}
}
}
}
}
class Y {
var counter = 0
def doSomething() : Unit = {
if ( counter < 2 ) {
println("Tried to do something, but failed")
counter += 1
throw new RuntimeException
}
println("Did something")
}
}