groovy
  1. groovy
  2. GROOVY-3198

Equal instances of an @Immutable class give different values when used as map keys

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6-beta-2
    • Fix Version/s: 1.6-rc-1, 1.7-beta-1
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Two equal instances of an @Immutable class should produce the same value when used as a map key. This is not the case, even though their hash codes are equal.
      The last assert below fails, though it should succeed.

      @Immutable
      final class Foo {
          String bar
      }
      
      foo1 = new Foo('bar')
      foo2 = new Foo('bar')
      assert foo1 == foo2
      assert foo1.hashCode() == foo2.hashCode()
      fooMap = [(foo1) : 1]
      assert fooMap[foo1] == 1
      assert fooMap[foo2] == 1 // Fails!!!
      

        Activity

        Hide
        Paul King added a comment -

        Changed equals(ImmutableClass other) to equals(Object other) which seems to fix the problem. I had assumed we could get away without this with bridging but appears not to be the case.

        Show
        Paul King added a comment - Changed equals(ImmutableClass other) to equals(Object other) which seems to fix the problem. I had assumed we could get away without this with bridging but appears not to be the case.

          People

          • Assignee:
            Paul King
            Reporter:
            Tord Alenljung
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: