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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With