Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8182323

Add Hashable interface and compiler checking for hashCode/equals contract

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      If the compiler is (optionally) checking hashing in a given project/module/package, each class therein that is passed to code that contains (anywhere) a call to hashCode must have an explicit, non-inherited implementation of both equals() and hashCode() OR the explicit annotation @InheritHashEquals. Under this system, nobody can accidentally put un-hashable objects into HashMaps or HashSets or indeed any code that relies on hashing. The compiler can immediately warn the user instead of absorbing hours of their time searching for subtle collection bugs. The idea may need some work, but it's a nasty problem that even after 20+ years still needs to be addressed if at all possible.

      JUSTIFICATION :
      Just spent 3 hours this morning debugging a problem due to a missing but completely trivial hashCode/equals implementation. Ugh.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compiler should (optionally) check hash/equals and warn.
      ACTUAL -
      Subtle collection failures.

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: