News for hackers. Stuff that matters.
Re:How To Write an Equality Method in Java by req2 on Tuesday June 09, 2009 @04:09PM (#649869)
> In fact, it's well known in Java that hashCode and equals should always be redefined together.

Shouldn't there be some sort of in-language capacity to address this? A "Warning: equals and hashCode not codefined" seems like it would help.

Re:How To Write an Equality Method in Java by stcredzero on Tuesday June 09, 2009 @04:13PM (#649880)
How would you know if the inherited hashCode is correct or not? Would you just assume that it isn't? That would result in lots of redundantly defined hashCode methods. Not sure that's better.
Re:How To Write an Equality Method in Java by req2 on Tuesday June 09, 2009 @04:58PM (#649986)
I'm over my head here, but this is the naive idea that I imagined -

You could add two keywords like /notice/ and /keep/. If a parent class's function definition of foo has /notice bar/, overrides of foo could allow the new class to inherit bar with /keep bar/, or warn if neither a keep statement or an override are present.

Re:How To Write an Equality Method in Java by jeffesp on Tuesday June 09, 2009 @07:00PM (#650177)
I would think that if both equals and hashCode are not implemented in a specific class, you would issue a warning like the parent says. I'm not sure of the details, but I assume you can disable warning for regions of code like you can in C#. So when you know you did things right, you can tell the compiler (and other coders that follow you) you know what you are doing. If you didn't do things right then the compiler reminds you to check things out. It's a win in both situations.
Re:How To Write an Equality Method in Java by ilyak on Tuesday June 09, 2009 @05:10PM (#650006)
FindBugs does that.