Scala and == method in recursive defined types

Viewed 362

I know that the == method in Scala has the same semantics of the equals method in Java. However, I would like to understand when applied to instances of recursive structures. For instance, consider a bunch of expressions:

abstract class Exp

abstract class BinaryExp(l:Exp, r:Exp) extends Exp

case class Plus(l:Exp, r:Exp) extends BinaryExp(l,r)

case class Minus(l:Exp, r:Exp) extends BinaryExp(l,r)

case class Mult(l:Exp, r:Exp) extends BinaryExp(l,r)

case class Div(l:Exp, r:Exp) extends BinaryExp(l,r)

case class Num(v:Int) extends Exp

Then, when I have two instances of a BinaryExp, say obj1 and obj2, does obj1 == obj2 result in a deep (recursive) equality test? That is, is it guaranteed that if obj1 == obj2 holds, then obj1 and obj2 represent the same exact expression trees?

Note that in all classes, I am relying on the default implementation of == (it is not overridden anywhere).

1 Answers
Related