Reputation: 37
I've got this 'if' condition inside a recursion function :
if (tmpList.size == 14) {
val FBData = Entry (tmpList(0), tmpList(1),tmpList(2),tmpList(3),tmpList(4),tmpList(5).toInt,tmpList(6).toInt,tmpList(7).toInt,tmpList(8).toInt,tmpList(9).toInt,tmpList(10).toInt,tmpList(11).toInt,tmpList(12).toInt,tmpList(13).toInt)
if (index == lines.size - 1) {
List(FBData)
} else {
val Data = pRecurrsioon(lines, index + 1) ++ FBData.toList
}
}
When I runt this through a Scala compiler I get this issue:
31: error: type mismatch; found : Unit required: List[HelloWorld.Entry] }
I don't know why this keeps happening or how to fix it. Apparently im returning an Unsigned integer at some point but I can't see where.
Upvotes: 0
Views: 168
Reputation: 28680
In Scala, the body of the if
and the else
do return values. The following is also valid (and still has your bug):
val ifResult = if (index == lines.size - 1) {
//this works, and List(FBData) will be assigned to ifResult
List(FBData)
} else {
//this returns Unit as a result of the assignment operation
val Data = pRecurrsioon(lines, index + 1) ++ FBData.toList
}
The compiler tries to assign to ifResult
either the body of the if
or the else
. For this to work, it has to unify the types. This causes the error message you see, while checking the body of the else
and trying to get the same type of it as from the if
-body:
31: error: type mismatch; found : Unit required: List[HelloWorld.Entry] }
Upvotes: 2