[enhanced-enums]: Use case and rehabilitation?

B. Blaser bsrbnd at gmail.com
Wed Sep 13 20:46:28 UTC 2017


I'd like to examine a bit more the use case along with the blocking
reason expressed in [1].

As explained, the main problem in using enhanced enums within javac
options appears when invoking some methods like
"EnumSet.noneOf(Option.class)". "Option" being raw, all type
parameters are erased and the resulting type is only "EnumSet" which
is causing problems in some situations.

Another problem is in representing an heterogeneous set of options
using "EnumSet<Option<?>>" which is currently impossible, as explained
in [1].

But fortunately, those problems are not blocking when refactoring
javac options with enhanced enums.

Here are the main keys:

* "enum Option<X> {}" instead of "enum Option {}"
* "EnumSet<Option<?>>" which doesn't work becomes "Set<Option<?>>"
* "EnumSet.noneOf(Option.class)" which is raw becomes "new TreeSet<Option<?>>()"
* "EnumSet.allOf(Option.class)" which is also raw becomes "new

Then, we can start defining some options:

* "D<String>("-d", ...)"
* "PROC<ProcOption>("-proc:", ...)" with "enum ProcOption { none, only; }"
* "G_CUSTOM<EnumSet<DebugOption>>("-g:", ...)" with "enum DebugOption
{ lines, vars, source; }"
   notice that "EnumSet<DebugOption>" is well-formed here and there's
no problem in using it
* "RELEASE<Integer>("--release", ...) {
       @Override // Overrides "public <X> X parse(String arg)"
declared in "Option<X>"
       public Integer parse(String arg) {
           return new Integer(arg); // Simplified parsing

After parsing, we can provide them through "Options" and "OptionHelper":

* "public <Z> void put(Option<Z> option, Z value)"
* "public <Z> boolean isSet(Option<Z> option, Z value)"
* "public <Z extends Enum<Z>> boolean isSet(Option<EnumSet<Z>> option, Z value)"
* "public <Z> Z get(Option<Z> option)"

And finally, we can use them conveniently:

* "helper.put(lookup(primaryName), parse(arg))" in "Option"
* "options.isSet(Option.PROC, Option.ProcOption.only)" in
* "options.isSet(G_CUSTOM, DebugOption.vars)" in "Gen"
* "Integer platform = options.get(Option.RELEASE)" in "Arguments"

I've tried successfully this refactoring on the latest JDK 10 using
the latest enhanced enums JDK.

Do you think this would be interesting to rehabilitate enhanced enums
and continue exploring them?


[1] http://mail.openjdk.java.net/pipermail/amber-spec-experts/2017-May/000041.html

More information about the amber-dev mailing list