Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: type mismatch; found : Unit required: Boolean

Tags:

syntax

scala

Hi I'm just trying out my first bits of scala and have hit this error which I don't understand. I've been trying to work it out and have exhausted my ideas. Help?

scala> def calculate(count: Int) : Boolean =    
     |           if (count<0) false
<console>:8: error: type mismatch;
 found   : Unit
 required: Boolean
                 if (count<0) false
                 ^

Thanks

like image 797
Inti Avatar asked Sep 22 '12 00:09

Inti


1 Answers

You have to have an else clause, otherwise the type checker doesn't know what the return type is when it's not the case that count<0.

def calculate(count: Int): Boolean =    
  if (count<0) false
  else true

Or, better yet, you don't need the if-statement at all:

def calculate(count: Int) = count >= 0
like image 93
dhg Avatar answered Sep 24 '22 08:09

dhg