JEP 301: Enhanced Enums
Remi Forax
forax at univ-mlv.fr
Wed Dec 7 09:03:36 UTC 2016
I don't really like this proposal, it tries to resolve a corner case but breaks an important feature of the enum, the type of the values of an enum is the same as the enum.
Enums are a convenience feature, not something that should be used to design a hierarchy of types because, the hierarchy is closed and designing a hierarchy with an abstract base class is so 2000 .
We now have lambdas, so there is no reason to use 'abstract' enums anymore.
Instead of
interface Activity {
void perform();
}
enum Activities implements Activity {
FOO {
public void perform() { /* FOO */ }
},
BAR {
public void perform() { /* BAR */ }
}
}
it's better to write:
interface Activity {
void perform();
}
enum Activities implements Activity {
FOO(() -> { /* FOO */ }),
BAR(() -> { /* BAR */ });
private final Runnable runnable;
private Activities(Runnable runnable) {
this.runnable = runnable;
}
public void perform() {
runnable.run();
}
}
Here, you have only one class at compile time, the lambdas are created at runtime and the way the hierarchy is implemented is hidden from the public API.
I think the GoF name this design pattern as the external decorator or something like this and it's 'use delegation instead of inheritance' to its full glory.
Now, If this proposal is accepted, at least, instead of changing the spec to introduce sharp types for all kind of enums, adding sharp type only for generics enums,
it will break no code because generics enum currently do not compile.
regards,
Rémi
----- Mail original -----
> De: "mark reinhold" <mark.reinhold at oracle.com>
> À: "maurizio cimadamore" <maurizio.cimadamore at oracle.com>
> Cc: platform-jep-discuss at openjdk.java.net
> Envoyé: Mercredi 7 Décembre 2016 00:49:01
> Objet: JEP 301: Enhanced Enums
> New JEP Candidate: http://openjdk.java.net/jeps/301
>
> - Mark
More information about the platform-jep-discuss
mailing list