I have a method below (note I have simplified to make question simpler)
def getMyInt(flag1: Boolean, flag2: Boolean): Int = {
if (flag1) 0;
else {
if (flag2) 1;
if (flag2) 2;
}
}
Problem is that even though inner if expression returns Ints, the else that wraps will return Unit. And hence I get:
Multiple markers at this line
- type mismatch; found : Unit
required: Int
Any tips?
ifexpression has noelsepart, it's as if you wroteif (cond) value else ()(the()is the literal notation for theUnitvalue). The type of anifexpression is the LUB (most specific common supertype) of each of its "sides."