question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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() and hash 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:

  1. Is this implemented by the compiler? It’s probably possible to just make this the behavior of Object.equals() and Object.hash using the metamodel.
  2. If it’s generated by the compiler: What if there are identifying attributes, but equals() or hash was also overridden?
    1. Always disallow that. Possibly inconvenient when you inherit a class with identifying attributes, but want to implement your own equals().
    2. Only allow it if both equals() and hash are overridden, make it an error to override only one of them.
    3. Allow it. If only one is overridden, keep the “clever” version of the other.
    4. Allow it. If only one is overridden, fall back to the “dumb” version of the other.

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:open
  • Created 9 years ago
  • Comments:40 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
gavinkingcommented, Sep 24, 2018

Sorry, I mean to say:

identifying => [x, y, label];

Is a good approach, I think, generating equals() and hash from that.

1reaction
ePaulcommented, Sep 25, 2018

@xkr47 I guess just overwriting the hash attribute as hash => [v1].hash would be enough here?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found