Let's assume we have the following Action in PlayFramework 2.* controller:
def action = Action { request =>
    if (true) {
      return BadRequest("bad")
    }

    Ok("all ok")
}
What will be displayed as the result of requesting the action?
Explanation
Compilation Error:
method action has return statement; needs result type
From Scala Reference:
Syntax:
    Expr1      ::=  ‘return’ [Expr]
A return expression return e must occur inside the body of some enclosing named method or function. The innermost enclosing named method or function in a source program, must have an explicitly declared result type, and the type of e must conform to it.
Theory
  • The return statement from Java signals a side-effect - unwind the stack and give this value to the caller. In a language in which the emphasis is on side-effect-full programming, this makes sense. However Scala is an expression-oriented language in which the emphasis is on controlling/limiting side-effects and return is not idiomatic.
    To make matters worse, return probably doesn't behave as you think it does.
    A return statement inside a nested anonymous function is implemented by throwing and catching a NonLocalReturnException.
    Besides, return is anti-structural programming, as functions can be described with multiple exit points and if you need return, like in those gigantic methods with lots of if/else branches, the presence of a return is a clear signal that the code stinks, a magnet for future bugs and is thus in urgent need of refactoring.
    Read more
  • Syntax:
        Expr1 ::=  ‘return’ [Expr]
    
    A return expression return e must occur inside the body of some enclosing named method or function. The innermost enclosing named method or function in a source program, f, must have an explicitly declared result type, and the type of e must conform to it. The return expression evaluates the expression e and returns its value as the result of f. The evaluation of any statements or expressions following the return expression is omitted. The type of a return expression is scala.Nothing.
    The expression e may be omitted. The return expression return is type-checked and evaluated as if it was return().
    Read more, section 6.20.
  • Syntax:
        Expr1 ::=  ‘return’ [Expr]
    
    An apply method which is generated by the compiler as an expansion of an anonymous function does not count as a named function in the source program, and therefore is never the target of a return expression.
    Returning from a nested anonymous function is implemented by throwing and catching a scala.runtime.NonLocalReturnException. Any exception catches between the point of return and the enclosing methods might see the exception. A key comparison makes sure that these exceptions are only caught by the method instance which is terminated by the return.
    If the return expression is itself part of an anonymous function, it is possible that the enclosing instance of f has already returned before the return expression is executed. In that case, the thrown scala.runtime.NonLocalReturnException will not be caught, and will propagate up the call stack.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback