Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using the return statement and defaulting to return the last value?

I am learning Scala and I noticed something about using the return statement.

So, obviously in Scala, if you don't have a return statement, the last value is returned by default. Which is great. But if you use the return statement without specifying the return type, Scala says "error: method testMethod has return statement; needs result type"

So this works

  def testMethod(arg: Int) = {
    arg*2
  }

but this gives the error

  def testMethod(arg: Int) = {
    return arg*2
  }

This makes me scratch my chin and go

Mmmmmmmm... There must be a reason for that.

Why is the explicit type declaration needed when you use a return statement but not when you let Scala return the last value? I assumed they were exactly the same thing, and that return statements are just for if you want to return a value inside a nested function/conditional, etc. (In other words, that a "return" statement is automatically inserted to your last value by the compiler.. if not present anywhere else in the method)

But clearly I was wrong. Surely there must be some other difference in the implementation?

Am I missing something?

like image 393
Marco Prins Avatar asked Oct 31 '14 12:10

Marco Prins


2 Answers

If you have multiple exit points from your method, it may be impossible for the compiler to infer the return type. The only way you can get multiple exit points from your method, is by using multiple returns. (Or one return plus the implicit return.)

Since the rules, when it works and when it doesn't, are pretty complex, it may look to the user that it sometimes randomly works and sometimes doesn't. Simply forbidding type inference altogether in the presence of an explicit return is a much simpler rule, even if a bit restrictive.

like image 136
Jörg W Mittag Avatar answered Oct 16 '22 04:10

Jörg W Mittag


This question was "When is a return type required for methods in Scala?" A different question entirely, but it's in the same realm as OP.


This answer explains when (not why) you have to give it a return type:

https://stackoverflow.com/a/3127260/2567273

The Chapter 2. Type Less, Do More of the Programming Scala book mentions:

When Explicit Type Annotations Are Required.

In practical terms, you have to provide explicit type annotations for the following situations:

Method return values in the following cases:

  • When you explicitly call return in a method (even at the end).
  • When a method is recursive.
  • When a method is overloaded and one of the methods calls another. The calling method needs a return type annotation.
  • When the inferred return type would be more general than you intended, e.g., Any.

This doesn't explain why you have to specify a return (only when), but I'll stick to what I said in my comment. The reason you have to specify a return type when you use the return keyword is because the compiler is going to look for that you gave it a return type or else it will infer the return type on its own as if you didn't use return at all. If you specifically use return you're helping the compiler out by telling it what to return and then it expects you to tell it what type you are returning.

like image 29
Luminous Avatar answered Oct 16 '22 04:10

Luminous