'Why can't enum and record be combined?

Recently I was just creating another enum type. I took advantage of the fact that in Java, an enum is a special type of class (and not a named integer constant, like in C#). I made it with two fields, an all-arg constructor and getters for both fields.

This is an example:

enum NamedIdentity {

    JOHN(1, "John the Slayer"),
    JILL(2, "Jill the Archeress");

    int id;
    String nickname;

    NamedIdentity(int id, String nickname) {
        this.id = id;
        this.nickname = nickname;
    }

    id id() {
        return this.id;
    }

    String nickname() {
        return this.nickname;
    }
}

Then I thought that Java 14's record keyword would have saved me the boilerplate code that this feature was trying to save me. This, as far as I know, doesn't combine with enums. If enum records would have existed, the abovementioned code would then look like this:

enum record NamedIdentity(int id, String nickname) {
    JOHN(1, "John the Slayer"),
    JILL(2, "Jill the Archeress");
}

My question is: is there a reason that enum records don't exist? I could imagine several reasons, including, but not limited to:

  • The number of use-cases for this feature would be too small, the Java language would benefit more if we, as Java language designers, designed and implemented other features.
  • This is hard to implement, due to the internal implementation of the enum type.
  • We, as Java language designers, simply didn't think about this, or we have not received such a request from the community yet, so we didn't prioritize it.
  • This feature would have semantical issues, or the implementation of this feature could cause semantic ambiguity or otherwise confusion.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source