我真的不明白为了让scala理解我的函数最终会输出正确的类型而不是单位而需要布置函数和部分函数的顺序。对于 eclipse 在这些行中告诉我的内容,我已经发表了评论(第 7 行和最后一行)。非常感谢对编译器的任何见解。
object braces {
def balance(chars: List[Char]): Boolean = {
def rightBraces(chars: List[Char], openCount: Int, closeCount: Int): Boolean = {
if (!chars.tail.isEmpty) {
if (chars.head == '(') rightBraces(chars.tail, openCount + 1, closeCount)
/*type mismatch; found : Unit required: Boolean*/else if (chars.head == ')') rightBraces(chars.tail, openCount, closeCount + 1)
}
else {
if ((chars.head == '(')&&(openCount == (closeCount - 1))) true
else if ((chars.head == ')')&&(openCount == (closeCount + 1))) true
else (openCount == closeCount)
}
}
def wrongBrace(chars: List[Char]): Boolean = {
if (!chars.tail.isEmpty) {
if (chars.head == ')') false
else if (chars.head == '(') rightBraces(chars.tail, 1, 0)
else wrongBrace(chars.tail)
}
else false
}
wrongBrace(chars)
}
}//Missing closing brace `}' assumed here