Questions about Specialization

Frédéric Martini frederic.martini at gmail.com
Tue Aug 26 05:20:01 UTC 2014


Thanks for your response.




2. About hashCode()/equals()/toString() :

When we use hashcode()/equals()/toString()/compareTo(), conditional
methods will require
to duplicate code for each primitive type, in order to call the
appropriate method :

public class Box<any T>
    private T value;

    <when reference T> public int hashCode() {
          return value.hashCode();
    }

    <when T=int> public int hashCode() {
         return Integer.hashCode(value);
    }

    <when T=long> public int hashCode() {
         return Long.hashCode(value);
    }

    <when T=char> public int hashCode() {
         return Character.hashCode(value);
    }
    // etc.

I think it would be more practical to allow the invocation of some
methods on primitive types :

   int i = 10;
   i.hashCode()        // compiled as Integer.hashcode(i);
   i.equals(10)        // compiled as i==10
   i.toString()        // compiled as Integer.toString(i);
   i.compareTo(10)    // compiled as Integer.compare(i, 10)


I think this is not so hard to implement (it's a compile-time syntaxic
sugar) and it would
greatly reduce the number of conditional methods.



Fred.

2014-08-25 23:23 GMT+02:00 Brian Goetz <brian.goetz at oracle.com>:
>> 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).
>
>
> Currently, yes; of course this may change.
>
>
>> 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)"...
>
>
> Open question; the answer likely will come from conditional methods ("where"
> methods).
>
>
>> 3. I suppose it's forbidden to use the "any" modifier with variance, eg :
>
>
> Yes.  Also with bounds.
>
>
>> 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 :
>
>
> Yes, it could be useful; it could also add excessive complexity, so we're
> doing the prudent thing and only biting off what we can currently chew.
> We'll return to these questions when we're deeper in the exploration of
> value types (related).
>


More information about the valhalla-dev mailing list