In Scala , why would such an expression return AnyVal? (I was expecting a boolean)
if (expr0)
if ((expr1)&&(expr2))
true
else if (expr3)
false
else
recFun(arg0, arg1)
Note 1: recFun is a recursive function...
Note 2: It is the last line of the recursive function itself...
You are using an expression
if
with no partelse
, which is equivalent to:That is, when
expr0 == false
, the result ofif
is()
, of typeUnit
. In total, all of theif
ends up having the typeAnyVal
, being the common supertypeBoolean
of andUnit
.If performance is not a major problem, the best thing you can use for the problem you have is pattern matching, it gives you a much cleaner code.