JEP 301: Enhanced Enums
forax at univ-mlv.fr
forax at univ-mlv.fr
Thu Dec 8 22:36:24 UTC 2016
----- Mail original -----
> De: "John Rose" <john.r.rose at oracle.com>
> À: forax at univ-mlv.fr
> Cc: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>, platform-jep-discuss at openjdk.java.net
> Envoyé: Jeudi 8 Décembre 2016 22:12:14
> Objet: Re: JEP 301: Enhanced Enums
> On Dec 7, 2016, at 11:23 PM, forax at univ-mlv.fr wrote:
>>
>> Name functions are not useful anymore, because now we have full real lambdas.
>
> There is still a big place for named function enums. Lambdas let you say "here
> is a plus operator" but not "here is the plus operator which is abelian with
> unit 0". Enums can fill that gap, for now.
you mean something like:
interface Group<T> {
BinaryOperator<T> binOp();
T identity();
boolean is(Characteritics characteristics);
..
}
enum Op<T> implements Group<T> {
PLUS<Integer>(Integer::sum, 0, Characteritics.ABELIAN)
;
...
}
public static <T> Optional<T> reduce(List<? extends T>list, Group<T> group) {
if (group.is(Characteritics.ABELIAN) && list.size() > ...) {
return Optional.of(parallelReduce(list, group.identity(), group.binOp()));
}
return foldLeft(list, group.binOp());
}
...
reduce(List.of(1, 2, ...), Op.PLUS);
but you can simply write
static class Op<T> implements Group<T> {
public static final Group<Integer> PLUS = new Op(Integer::sum, 0, Characteritics.ABELIAN);
...
}
>
> – John
Rémi
More information about the platform-jep-discuss
mailing list