0

I have the following recursive call written in 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
}

The commented line is throwing type mismatch error and saying Found: Anyval Required:Int.

Can anyone please tell what is the problem here?

1
  • Could you please share all the code necessary to reproduce the problem? - BTW, you should not call isInstanceOf in Scala, we have better tools like pattern matching for that. Commented Dec 5, 2019 at 16:43

1 Answer 1

2

You need a final else clause to return the default value if t is not Leaf or OtherNode.

A match expression would be better than using isInstanceOf calls.

Sign up to request clarification or add additional context in comments.

2 Comments

So, scala will throw error if there is no final else statement unlike Java where you could write this same code without specifying a final else.
It will not throw an error, the problem is type of if without else is Unit (type of if with else is a common super-type of both possible results).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.