Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Scala function returning type Unit and not whatever is the last line?

Tags:

function

scala

I am trying to figure out the issue, and tried different styles that I have read on Scala, but none of them work. My code is:

....

val str = "(and x y)";

def stringParse ( exp: String, pos: Int, expreshHolder: ArrayBuffer[String], follow: Int )  

    var b = pos; //position of where in the expression String I am currently in
    val temp = expreshHolder; //holder of expressions without parens
    var arrayCounter = follow; //just counts to make sure an empty spot in the array is there to put in the strings

    if(exp(b) == '(') {
        b = b + 1;

        while(exp(b) == ' '){b = b + 1} //point of this is to just skip any spaces between paren and start of expression type

        if(exp(b) == 'a') {
               temp(arrayCounter) = exp(b).toString; 
               b = b+1; 
               temp(arrayCounter)+exp(b).toString; b = b+1; 
               temp(arrayCounter) + exp(b).toString; arrayCounter+=1}
               temp;

         }

}

val hold: ArrayBuffer[String] = stringParse(str, 0, new ArrayBuffer[String], 0);
for(test <- hold) println(test);

My error is:

Driver.scala:35: error: type mismatch;
found   : Unit
 required: scala.collection.mutable.ArrayBuffer[String]
ho = stringParse(str, 0, ho, 0);
                ^one error found

When I add an equals sign after the arguments in the method declaration, like so:

def stringParse ( exp: String, pos: Int, expreshHolder: ArrayBuffer[String], follow: Int )  ={....}

It changes it to "Any". I am confused on how this works. Any ideas? Much appreciated.

like image 757
Andy Avatar asked Apr 04 '12 05:04

Andy


People also ask

Why is my function returning a Unit in Scala?

In Scala we use the Unit return type to indicate "no return value." This is a "void" function. The void keyword is not used. Return An empty "return" statement can be used in a method that returns a Unit type. This means "no value."

What is Unit return type 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 the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .


1 Answers

Here's a more general answer on how one may approach such problems:

It happens sometimes that you write a function and in your head assume it returns type X, but somewhere down the road the compiler disagrees. This almost always happens when the function has just been written, so while the compiler doesn't give you the actual source (it points to the line where your function is called instead) you normally know that your function's return type is the problem.

If you do not see the type problem straight away, there is the simple trick to explicitly type your function. For example, if you thought your function should have returned Int, but somehow the compiler says it found a Unit, it helps to add : Int to your function. This way, you help the compiler to help you, as it will spot the exact place, where a path in your function returns a non-Int value, which is the actual problem you were looking for in the first place.

like image 104
Frank Avatar answered Nov 14 '22 21:11

Frank