Questions about Specialization

Frédéric Martini frederic.martini at gmail.com
Mon Aug 25 21:12:45 UTC 2014


Hello,

First, sorry for my poor english, and I hope this message is appropriate...



I've read the "State of the Specialization" :
http://cr.openjdk.java.net/~briangoetz/valhalla/specialization.html
And I have some question...





1. Could you confirm me that, if I have a specialized type Box<any T> :
Box<T> will always use reified type when T is a primitive or a type-value.
Box<T> will always use erasure when T is an object type
(interface/enum/class).





2. What's about using hashCode()/equals()/toString() in Box<any T> ?
Exemple :

public class Box<any T> {

    private T value;

    @Override
    public int hashCode() {
        return value.hashCode();
    }
}

Is it possible ?
Eg with Box<int> "value.hashCode()" will be corrected to
"Integer.hashCode(value)"...






3. I suppose it's forbidden to use the "any" modifier with variance, eg :

public class Box<any T extends Iterable<T>> { // compile error ?

But what's about Comparable, Serializable or Cloneable ?
I think it could be useful to do something like this :

public class Box<any T extends Comparable<T> & Serializable & Cloneable>
    implements Comparable<Box<T>>, Serializable, Cloneable {
    private T value;
}

In fact all primitives ares comparable, serializable and cloneable... So
this might be legal after all.






4. I see we can use specialization-specific on method for specific
implementation for reference or a specific primitive type.
But it is possible to use it with a java type ?

It would be interesting for java.util.List and sort() :

public interface List<E> {

    <when T extends Comparable<T>
    public default void sort() {
        this.sort(Comparator.naturalOrder());
    }

    default void sort(Comparator<? super E> c) {
        ...
    }
}

So :

List<String> strings = ...
strings.sort(); // OK

List<JButton> buttons = ...
buttons.sort(); // Compile error : JButton don't extends Comparable<JButton>


I think this could be a compile-time check, so even "classic" Generics type
could use that.



Thanks for reading, and replying.


Fred,



More information about the valhalla-dev mailing list