What is the difference between using the return statement and default 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. It's 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 it works

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

but it gives an error

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

It makes me scratch my chin and go

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

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

But obviously, I was wrong. Surely there must be some other difference in implementation?

Did I miss something?

+5
source share
3 answers

If you have multiple exit points from your method, it may not be possible for the compiler to infer the type of the return value. The only way to get multiple exit points from your method is to use multiple return s. (Or one return plus an implicit return.)

Since the rules when it works, and when not, are quite complex, it may look like for a user, who sometimes works by chance, and sometimes not. Simply prohibiting type inference in general when there is an explicit return is a much simpler rule, even if it is a bit limited.

+4
source

This question was "When is a return type required for methods in Scala?" The other question is complete, but it is in the same area as the OP.


This answer explains when (and not why) you should give it a return type:

fooobar.com/questions/344989 / ...

Chapter 2. Type β€œLess, Do More. Scala Programming :

When annotations of an explicit type are required.

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

Returns the values ​​of the method in the following cases:

  • When you explicitly call return in a method (even at the end).
  • When the method is recursive.
  • When a method is overloaded and one of the methods calls the other. The calling method requires an annotation of the type of the return value.
  • When the return type will be more general than you expected, for example, Any .

This does not explain why you should indicate a refund (only when), but I will stick to what I said in my comment. The reason you need to specify the type of the return value when using the return keyword is because the compiler will look for you to give it the return type, otherwise it will output the return type yourself, as if you would not use return at all. If you specifically use return , you help the compiler by telling it what to return, and then expects you to tell what type you return.

+1
source

What do you think, should the type of output print the type of the return value:

 def func(n:Int) = { if(n<10) return 23; if(n>10) true else false; } 

The first line assumes that the return value must be of type Int . But implicit return assumes a logical meaning. Therefore, it becomes quite controversial what exactly should be the type of return. Int , Boolean or Any ?

If an explicit return were not used, it is quite simple to determine the return type of the function. But with that, he is a little vague and unknown. The simplest solution would be to explicitly specify the return type. This greatly simplifies the life of the compiler.

In addition, the team wanted to discourage the use of explicit return types, so people who set manual types come with the intention :)

+1
source

Source: https://habr.com/ru/post/1205954/


All Articles