Variants/case classes/algebraic data types/sums/oh my!

forax at univ-mlv.fr forax at univ-mlv.fr
Sun Jun 12 18:12:35 UTC 2016


----- Mail original -----

> De: "Vitaly Davidovich" <vitalyd at gmail.com>
> À: "John Rose" <john.r.rose at oracle.com>
> Cc: "Rémi Forax" <forax at univ-mlv.fr>, valhalla-dev at openjdk.java.net
> Envoyé: Dimanche 12 Juin 2016 19:30:21
> Objet: Re: Variants/case classes/algebraic data types/sums/oh my!

> On Saturday, June 11, 2016, John Rose < john.r.rose at oracle.com > wrote:

> > On Jun 11, 2016, at 5:12 PM, forax at univ-mlv.fr wrote:
> 
> > >
> 
> > >
> 
> > > De: "John Rose" < john.r.rose at oracle.com >
> 
> > > À: "Rémi Forax" < forax at univ-mlv.fr >
> 
> > > Cc: "org openjdk" < org.openjdk at io7m.com >, valhalla-dev at openjdk.java.net
> 
> > > Envoyé: Dimanche 12 Juin 2016 01:02:52
> 
> > > Objet: Re: Variants/case classes/algebraic data types/sums/oh my!
> 
> > > Hi John,
> 
> > >
> 
> > >>> On Jun 11, 2016, at 4:18 AM, Remi Forax < forax at univ-mlv.fr > wrote:
> 
> > >>>
> 
> > >>> because K is reified, there will be two methods eval() at runtime, one
> > >>> specialized for K = 0 and an other for K = 1,
> 
> > >>> which means that the switch will be constant fold (the real 'switch' is
> > >>> done when selecting the specialization).
> 
> > >>>
> 
> > >> That is an interesting use case for non-type class template parameters!
> 
> > >>
> 
> > > Here is another one,
> 
> > > class ArrayList<any E> {
> 
> > > final E[] array;
> 
> > > int size;
> 
> > >
> 
> > > public <Consumer<? super E> U> void forEach(U consumer) {
> 
> > > for(int i = 0; i < size; i++) {
> 
> > > consumer.accept(array[i]);
> 
> > > }
> 
> > > }
> 
> > > }
> 

> > I think there are two simple ways to interpret your suggestion. One is to
> > split and specialize forEach by concrete consumer *type*, and one is to
> > specialize by consumer *instance*.
> 

> > public <__SpecializePerType U extends Consumer<? super E>> void forEach(U
> > consumer) {
> 
> > for(int i = 0; i < size; i++) {
> 
> > consumer.accept(array[i]);
> 
> > }
> 
> > }
> 
> > public <__SpecializePerInstanceOf U extends Consumer<? super E>> void
> > forEach(U consumer) {
> 
> > for(int i = 0; i < size; i++) {
> 
> > consumer.accept(array[i]);
> 
> > }
> 
> > }
> 

> > > In the snippet above, forEach is specialized by the value of the lambda
> > > taken as argument, which is another way to say that he code of forEach
> > > and
> > > the code of the lambda should be inlined together.
> 

> > Yes; function parameters to templates are an important organizing feature
> > for
> > C++ STL algorithms. The sort method is also a good candidate for such
> > treatment. C++ expresses per-type specialization using type parameters and
> > per-instance specialization using non-type parameters.
> 
> Goes without saying, but non-type parameters are very useful to propagate
> constants, possibly quite "far" with sufficient inlining, and open up more
> optimization opportunities. I'd love something similar in Java.

It remember me that you can 'constantify' everything you want in Java 
https://gist.github.com/anonymous/7cabe890a28547dad0401dc7883cc4a7 

Rémi 


More information about the valhalla-dev mailing list