Enhanced Enums -- use cases
Brian Goetz
brian.goetz at oracle.com
Tue Apr 11 19:11:18 UTC 2017
As you may have noticed, we pushed an implementation of Enhanced Enums
some time ago. We're hoping to get user feedback on the feature as it
is now implemented.
To get things started, here are some typical use cases where generic
enums might be useful. Please contribute others, as well as places in
the JDK where code could be refactored using this feature.
An obvious example is com.sun.tools.javac.code.Dynamic
(http://hg.openjdk.java.net/valhalla/valhalla/langtools/file/85cc92a65da8/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Dynamic.java#l51),
where we have an enum that represents the kinds of types representable
in the constant pool.
In that case, these factory methods:
http://hg.openjdk.java.net/valhalla/valhalla/langtools/file/85cc92a65da8/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Dynamic.java#l137
should be collapsible to a single generic method:
<D> static BootstrapArgument<D> constant(Kind<D> kind, D data).
Another example is command line parsing; for command line arguments, we
can represent them as enums whose type represents the type of an
(optional) parameter:
enum Args {
QUIET<??>("quiet", "q"),
FILE<String>("file", "f"),
COUNT<Integer>("count", "c"), ....
}
(though its not obvious what the type arg of QUIET should be.)
If anyone wants to experiment and offer their experience in applying (or
misapplying) this feature, either to the JDK or their own codebase, that
would be appreciated....
More information about the amber-dev
mailing list