斯卡拉中递归函数中的类型不匹配错误



我用 Scala 编写了以下递归调用:

def calculateFingerPrint(t: Tree):Int =
{
if(t.isInstanceOf[Leaf])
calculateIDS(t).hashCode()
else if(t.isInstanceOf[OtherNode])
//error --> calculateIDS(t).concat(calculateFingerPrint(t.children.head).toString()).hashCode 
}
def calculateIDS(t: Tree):String= {
//returns some string
}

注释的行抛出类型不匹配错误并说Found: Anyval Required:Int

谁能说出这里的问题是什么?

如果t不是LeafOtherNode,则需要一个 finalelse子句来返回默认值。

match表达式比使用isInstanceOf调用更好。

最新更新