0

我有以下用 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.

谁能告诉这里有什么问题?

4

1 回答 1

2

如果is not或,您需要一个 finalelse子句来返回默认值。tLeafOtherNode

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

于 2019-12-05T16:49:35.983 回答