Auto-generating equals and hash based on annotated attributes
See original GitHub issue[@lucaswerkmeister] I thought I’d open a dedicated issue for @gavinking’s suggestion here:
I would like to have a system based on annotations that produces
equals()
andhash
at once.
If I understand that correctly, you would annotate some attributes identifying
(or a different name), and you would get equals()
and hash
that used equals()
/ hash
of all attributes annotated identifying
.
Open questions:
- Is this implemented by the compiler? It’s probably possible to just make this the behavior of
Object.equals()
andObject.hash
using the metamodel. - If it’s generated by the compiler: What if there are
identifying
attributes, butequals()
orhash
was also overridden?- Always disallow that. Possibly inconvenient when you inherit a class with
identifying
attributes, but want to implement your ownequals()
. - Only allow it if both
equals()
andhash
are overridden, make it an error to override only one of them. - Allow it. If only one is overridden, keep the “clever” version of the other.
- Allow it. If only one is overridden, fall back to the “dumb” version of the other.
- Always disallow that. Possibly inconvenient when you inherit a class with
To make any sense, this probably requires identifying
to be an inherited
annotation. But in #4020 @FroMage said that actual
members can’t inherit annotations 😦
[Migrated from ceylon/ceylon-spec#971]
Issue Analytics
- State:
- Created 9 years ago
- Comments:40 (15 by maintainers)
Top Results From Across the Web
Auto-generating equals and hash based on annotated attributes ...
I would like to have a system based on annotations that produces equals() and hash at once. If I understand that correctly, you...
Read more >@EqualsAndHashCode - Project Lombok
Any class definition may be annotated with @EqualsAndHashCode to let lombok generate implementations of the equals(Object other) and hashCode() methods.
Read more >Generating equals / hashcode / toString using annotation
I'm using Project Lombok for this. ... While Pojomatic does not do compile-time bytecode manipulation, it does support easy creation of equals, ...
Read more >Ultimate Guide to Implementing equals() and hashCode() with ...
Here you can see an example that provides a equals() and hashCode() implementation based on the String businessKey attribute. I also annotated the ......
Read more >Groovy Goodness: Generate equals and hashcode Methods ...
To include calls to a super class we use the annotation attribute callSuper and assign the value true . Finally we can also...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, I mean to say:
Is a good approach, I think, generating
equals()
andhash
from that.@xkr47 I guess just overwriting the hash attribute as
hash => [v1].hash
would be enough here?