Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:08:08+00:00 2026-06-10T03:08:08+00:00

I know that the == method in Scala has the same semantics of the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:08:09+00:00Added an answer on June 10, 2026 at 3:08 am

    This is easy to test yourself:

    val x = Plus(Num(1), Num(2))
    val y = Plus(Num(1), Num(2))
    val z = Plus(Num(1), Num(3))
    
    println(x == y) // prints true
    println(x == z) // prints false
    

    The fact that these give the correct answers shows that the equality check is checking for “deep” equality of subexpressions.

    Furthermore, you can see in the documentation that:

    For every case class the Scala compiler generates equals method which implements structural equality

    “Structural equality” is the kind of deep equality checking that you are wondering about.

    Finally, if you really want to see what’s happening beyond on the syntactic sugar, you can use the option -xPrint:typer when you run scalac or start the REPL. If you use that option with the REPL and then declare the class Plus, here’s what you get (shortened):

    scala> case class Plus(l:Exp, r:Exp) extends BinaryExp(l,r)
    [[syntax trees at end of typer]]// Scala source: <console>
    ...
    case class Plus extends $line2.$read.$iw.$iw.BinaryExp with ScalaObject with Product with Serializable {
      ...
      override def equals(x$1: Any): Boolean = Plus.this.eq(x$1.asInstanceOf[java.lang.Object]).||(x$1 match {
        case (l: $line1.$read.$iw.$iw.Exp, r: $line1.$read.$iw.$iw.Exp)$line3.$read.$iw.$iw.Plus((l$1 @ _), (r$1 @ _)) if l$1.==(l).&&(r$1.==(r)) => x$1.asInstanceOf[$line3.$read.$iw.$iw.Plus].canEqual(Plus.this)
        case _ => false
      });
    

    So, buried in the first case you’ll see that Plus.equals is calling if l$1.==(l).&&(r$1.==(r)) in order to check equality. In other words, the generated equality method of a case class calls == on its subexpressions to check their equality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the HashSet.contains() method uses the .equals method to check equality as
We all know that openId has become such a popular method for user authentication,
I'm learning JUnit, I know that assertEquals() invokes the equals() method to compare objects...
We all know that the Control base class has a Scale method. The problem
I would like to know if it is possible to abstract the copy method
I would like to know what the best Scala imitation of Groovy's safe-dereference operator
I know that the -imageNamed: method returns a Cached UIImage, but the problem is
I know that when you write the initWithCoder method of a subclass of an
I know that I should have schema of a table before calling NewRow method
I want to know that is there any better method to do : var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.