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

  • SEARCH
  • Home
  • 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 399391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:50:31+00:00 2026-05-12T16:50:31+00:00

I was browsing scala tests and I don’t understand why the compiler produces a

  • 0

I was browsing scala tests and I don’t understand why the compiler produces a warning when you compare “two fresh objects”.

This is the test’ outputs:
http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/neg/checksensible.check

Example:

checksensible.scala:12: warning: comparing a fresh object using `!=' will always yield true
println(new Exception() != new Exception())
                        ^

If I write a class implementing an == method it will also produces this warning:

class Foo(val bar: Int) {
    def ==(other: Foo) : Boolean = this.bar == other.bar
}

new Foo(1) == new Foo(1)

warning: comparing a fresh object using `==' will always yield false

EDIT: Thanks oxbow_lakes, I must override the equals method, not the ==

class Foo(val bar: Int) {
    override def equals(other: Any) : Boolean = other match { 
        case other: Foo => this.bar == other.bar
        case _ => false
    }
}
  • 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-05-12T16:50:31+00:00Added an answer on May 12, 2026 at 4:50 pm

    Note that you should never override the == method (you should override the equals method instead). I assume that by a fresh object, Scala means a new object without a defined equals method.

    If you do not override equals, the == comparison is a reference comparison (i.e. using eq) and hence:

    new F() == new F()
    

    will always be false.

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

Sidebar

Related Questions

While browsing some code, I came across this line: if False: #shedskin I understand
While browsing the source code of the Scala API, I met this package definition
From browsing on this site and elsewhere, I've learned that serving websites as XHTML
While browsing I came across this blog post about using the Wikipedia API from
Browsing through Guava libraries I saw this weird signature on the readLines method of
While browsing System.Zip (Delphi XE2) to see how it works, I found this function:
While browsing some source code I came across a function like this: void someFunction(char
I have been browsing this site for the answer but I'm still a little
Browsing through the source code of Microsoft's sample StockTrader application, I found this snippet
Browsing through Coding Horror, I saw this article on removing the user field from

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.