Enhanced Enums -- use cases
Brian Goetz
brian.goetz at oracle.com
Wed Apr 12 14:21:59 UTC 2017
Reasonable ideas. (Note that implementing Enum methods is not simply a
matter of library code; the compiler generates them, so its a little
more complicated than "just add a method.")
On 4/12/2017 3:18 AM, Tagir Valeev wrote:
> Speaking about Enum enhancements, what about adding a new static
> method stream() which returns a stream of all enum values? Current
> alternatives are Stream.of(MyEnum.values()) and
> EnumSet.allOf(MyEnum.class).stream(), both allocating additional
> objects. Having dedicated MyEnum.stream() not only shorter, but could
> use internal Class.getEnumConstantsShared() directly. What do you think?
>
> With best regards,
> Tagir Valeev.
>
> On Wed, Apr 12, 2017 at 2:11 AM, Brian Goetz <brian.goetz at oracle.com
> <mailto:brian.goetz at oracle.com>> wrote:
>
> 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
> <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
> <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