[enhanced enums] - end of the road?

Peter Levart peter.levart at gmail.com
Thu Jun 15 13:07:04 UTC 2017



On 06/15/2017 02:44 PM, Peter Levart wrote:
>>
>> enum Option implements Consumer<String> {
>>    D implements Generic<String>("-d", ...) { ... }
>>    PROC implements Generic<ProcOption>("-proc", ...) { ... }
>>
>>    ...
>>
>> }
>>
>> Which is not too terrible (in fact has been put forward by John as a 
>> comment in the JEP [1]).
>>
>
> This is similar, but not the same. In above example, Generic<T> is not 
> a subtype of Option. It's just an interface implemented by constant's 
> subclasses. So you can not access Option members via an instance of 
> Generic<T>... Generic<T> therefore has to declare all the interesting 
> methods that can then be implemented by Option. You also have to 
> accompany this solution with "sealed" interfaces if you don't want 
> other implementations of Generic<T> besides the enum constants...

...in addition, you can not access/modify elements of EnumMap<Option, 
...> using keys of type Generic<T> for example - you would have to cast 
which is awkward given that your example use case:

public Z get(Generic<Z> option) { ... }

...would probably do just that...

Regards, Peter



More information about the amber-spec-experts mailing list