Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Type Mismatch found, Unit required: Boolean

I have been trying to run this code but somehow have hit the wall with 'unit mismatch, boolean expected error'. I have run through various questions on Stackoverflow but haven't found anything concrete that answers my question.

  def balance(chars: List[Char]): Boolean =
  {
    var i = 0;
    var j = 0;

    if (Count(i, j) == 0){
      true
    }
    else{
      false
    }

    def Count(count: Int, Pos: Int): Int = 
    {
            if (Pos == chars.length)
            {
                count
            }
            else
            {
                if (chars(Pos) == '(')
                {
                    Count(count + 1, Pos + 1);
                }
                else
                {
                    Count(count - 1, Pos + 1);
                }
            }
    }     
  } 
like image 405
user1343318 Avatar asked Feb 16 '23 03:02

user1343318


1 Answers

A code block delimited by {} evaluates to the last expression inside it. Here, your last expression is a definition (def Count), which evaluates to Unit. So move the expression you expect to be evaluated to the end.

like image 169
Luigi Plinge Avatar answered Feb 23 '23 15:02

Luigi Plinge