Question on layer/peeling

Michael Barker mikeb01 at gmail.com
Tue Jan 6 08:39:20 UTC 2015


>
> This trick is a good one, and it can go a bit farther with primitives, if
> you think about the other supertypes of Integer, Double, etc., especially
> Comparable.  (I'd kind of like to define an interface Objectable to carry
> toS/eq/hC.)
>

Could allowing primitives to implement interfaces like Comparable make the
following implementations be possible, such that the JIT could generate
specialisations for the primitive types?

public class Arrays
{
    public <any T extends Comparable> void sort(T[] array)
    {
        // ... implement sort
    }

    public <any T extends Number> double average(T[] array)
    {
        if (array.length == 0) { return Double.NaN; }

        double total = 0;
        int count = 0;
        for (T number : array)
        {
            total += number.doubleValue();
        }

        return total / count;
    }
}

int[] vals = { ... };
Arrays.<int>sort(vals);
double avg = Arrays.<int>average(vals);

If so, I'm sure that there could be some interesting applications.

Mike.



More information about the valhalla-dev mailing list