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 4568372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:03:59+00:00 2026-05-21T19:03:59+00:00

The code below generates: Name’s hashCode Name’s hashCode Name’s equals ID=0 import scala.collection.mutable object

  • 0

The code below generates:
Name’s hashCode
Name’s hashCode
Name’s equals
ID=0

import scala.collection.mutable
object TestTraits {
  def main(args: Array[String]): Unit = {
    val toto0 = new Person(0,"toto")
    val toto1 = new Person(1,"toto")
    val peoples = mutable.Set.empty[PersonID]
    peoples.add(toto0)
    peoples.add(toto1)
    peoples.foreach(_.showID)
    //peoples.foreach(_.saySomething)//won't compile'
  }
}

trait Name{
  var theName=""
  override def hashCode(): Int = {
    println("Name's hashCode")
    var hash = 5;
    hash = 71 * hash + this.theName.##;
    hash
    //super.hashCode()//infinite loop
  }

  override def equals(that: Any): Boolean = {
    println("Name's equals")
    that match {
    case that: Name     => this.theName.equals(that.theName)
    case _ => false
    }
  }
}

abstract class PersonID{
  val idNumber: Int

  override def hashCode(): Int = {
    println("PersonID's hashCode")
    super.##
  }
  override def equals(that: Any): Boolean = {
    println("PersonID's equals")
    that match {
    case that: PersonID     => this.eq(that)
    case _ => false
    }
  }
  def showID: Unit = {
    println("ID=" + idNumber)
  }
}

class Person(val id:Int, val s:String) extends {
  val idNumber=id
} with PersonID with Name {
  /*override def hashCode(): Int = {
    println("Person's hashCode")
    super.## //infinite loop !!
  }
  override def equals(that: Any): Boolean = {
    println("Person's equals")
    that match {
    case that: Person     => this.eq(that)
    case _ => false
    }
  }*/
  theName=s
  def saySomething: Unit = {
    print("Hello, my name is " + theName + ", ")
    showID
  }
}

Since “peoples” is a set of PersonID, I was expecting the following output:
PersonID’s hashCode
PersonID’s hashCode
ID=0
ID=1

Does someone can explain this behavior and how to do what I expected (that is, to have a class with “equals” based on values of fields except when putting the instance in a Set[PersonID])

Another mystery is why I get infinite loops when I use super.hashCode() in my custom hashCode ?

PS: I use a pre initialized abstract member because I need it in my real use case…

  • 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-21T19:04:00+00:00Added an answer on May 21, 2026 at 7:04 pm

    I get this instead:

    Name's hashCode
    Name's hashCode
    Name's equals
    ID=0
    

    This happens because Name is the last trait in the initialization, so it’s overrides of hashCode and equals will be the first to be called. You wanted Set to call methods based on the static type (ie, what has been declared), which is just not how OO works. If that were true, inheritance and overriding would be all but useless.

    As for how to accomplish what you want… you can’t. It would be nice if there was a Set which took an Equal[A] type class, but there isn’t. Maybe Scalaz has it.

    By the way, there’s a call to super.## which is deemed illegal on Scala 2.9.0.rc2. I’m not sure what that means yet.

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

Sidebar

Related Questions

I am editing a c# WinForm solution and I do not understand the code
I have a weird problem with my i18n doctrine schema and i.a. the admin
I have list of categories that is contained in li 's on one column
I'm struggling to figure out how to re-index (?) a series of hidden fields
I am trying to make Doctrine generate SQL from a set of models that
In PHP I am having difficulty trying to extract row data from one SQL
I have just started programming in Objective-C, I understand it only partially supports method
Is there a standard way to convert between TVarRec and Variant values? I want
I have developed an XSL file that transforms xml files into a html table.
I wonder if someone can help me. I'm using VS2010, .Net 4, EF4 and

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.