Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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.



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.


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.


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.


FindBugs does that.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: