我用 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
不是Leaf
或OtherNode
,则需要一个 finalelse
子句来返回默认值。
match
表达式比使用isInstanceOf
调用更好。