Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return in Scala

Tags:

return

scala

I am a newbie scala programmer and came across a weird behavior.

def balanceMain(elem: List[Char]): Boolean =   {     if (elem.isEmpty)       if (count == 0)         true;       else false;      if (elem.head == '(')       balanceMain(elem.tail, open, count + 1);.... 

Above basically I want to return true if elem.isEmpty and count == 0. Otherwise, I want to return false.

Now above I have read that there is no need to add a return statement in scala. So I have omitted return above. But it doesn't return the boolean. If I add a return statement as return true. it works perfectly. Why is it so?

Also, why is it considered a bad practice to have return statements in scala

like image 924
Jatin Avatar asked Sep 24 '12 07:09

Jatin


People also ask

Can you use return in Scala?

The Scala programming language, much like Java, has the return keyword, but its use is highly discouraged as it can easily change the meaning of a program and make code hard to reason about.

What is the return type of a procedure in Scala?

If you don't specify any return type of a function, default return type is Unit which is equivalent to void in Java. = : In Scala, a user can create function with or without = (equal) operator. If the user uses it, the function will return the desired value.

What is return keyword?

The return keyword finished the execution of a method, and can be used to return a value from a method.


1 Answers

It's not as simple as just omitting the return keyword. In Scala, if there is no return then the last expression is taken to be the return value. So, if the last expression is what you want to return, then you can omit the return keyword. But if what you want to return is not the last expression, then Scala will not know that you wanted to return it.

An example:

def f() = {   if (something)     "A"   else     "B" } 

Here the last expression of the function f is an if/else expression that evaluates to a String. Since there is no explicit return marked, Scala will infer that you wanted to return the result of this if/else expression: a String.

Now, if we add something after the if/else expression:

def f() = {   if (something)     "A"   else     "B"    if (somethingElse)     1   else     2 } 

Now the last expression is an if/else expression that evaluates to an Int. So the return type of f will be Int. If we really wanted it to return the String, then we're in trouble because Scala has no idea that that's what we intended. Thus, we have to fix it by either storing the String to a variable and returning it after the second if/else expression, or by changing the order so that the String part happens last.

Finally, we can avoid the return keyword even with a nested if-else expression like yours:

def f() = {   if(somethingFirst) {     if (something)      // Last expression of `if` returns a String      "A"     else      "B"   }   else {     if (somethingElse)       1     else       2      "C"                // Last expression of `else` returns a String   } 

}

like image 188
dhg Avatar answered Sep 23 '22 12:09

dhg