experiences with prototype

Peter Levart peter.levart at gmail.com
Tue Feb 3 18:30:50 UTC 2015


Hi,

I wanted to see what it would take to any-fy a basic subset of java 
collections library. I thought I would start with basic interfaces 
(Iterator, Iterable, Collection, List, ...) and then add basic 
implementations. I quickly learned that any-fying is a viral process. 
Any dependency that uses a generic type variable from any-fied class has 
to be any-fied too. So I had to make a cut somewhere or I would be 
any-fying the whole JDK (exaggerating) :-o ... I removed the methods 
that have connection to Streams API (stream(), splitterator(), ...) and 
parallel methods (FJPool) and got a manageable isolated subset. I though 
I was mostly finished with basic interfaces (which are not just 
interfaces as from Java 8 on they contain implementation too) as I fixed 
the last compilation error when javac crashed.

Since I can't easily isolate the part that crashes javac, here are the 
sources (19 classes) that I'm trying to compile in one go:

http://cr.openjdk.java.net/~plevart/misc/valhala-hacks/javany-src.jar

Perhaps Maurizio could look at it.

In general, the process of any-fying up to this point went mostly 
straightforward. A couple of pain points I encountered are:


__WhereVal(T)/__WhereRef(T) only works when 'T' is a type variable from 
enclosing generic class. It doesn't work for example with type variables 
from generic methods.


The Object-like methods (equals, hashCode, toString) work for <any T> 
typed receiver, but not when enclosed in __WhereVal(T) { ... } block. 
Why this restriction?


Especially tricky for any-fying was the following AbstractCollection method:

public abstract class AbstractCollection<any E> implements Collection<E> {
     public <any T> T[] toArray(T[] a)

...as it must support things like:

Collection<int> intCol = ...;

long[] longArray = intCol.toArray(new long[0]);

The tricky part was how to code a conversion from values of  type E to 
values of type T when E and T are both any type variables. The 
conversions one would like to support are: identity, widening, boxing, 
unboxing. These tricks are all hacked in javany.util.Any class If 
anybody is interested.

Currently there's no support in prototype to solve the List.remove(int) 
problem, so in my experiment I just renamed the method to removeByIndex.

All in all I was surprised with the level of completeness in the 
prototype as early as this. I'll try to go further with this anyfication 
experiment when the compiler lets me...


Regards, Peter



More information about the valhalla-dev mailing list