Received on valhalla-spec-comments: Generics and null

Brian Goetz brian.goetz at oracle.com
Wed Jan 3 20:29:19 UTC 2018


The following was received on valhalla-spec-comments (archived at 
http://mail.openjdk.java.net/pipermail/valhalla-spec-comments/2017-December/000002.html). 


Yes, such an operation is necessary both for generic code ("return the 
default value for the type corresponding to type variable T"), and is 
also useful for non-generic code ("give me the default value for int").  
The early Valhalla prototypes implement the syntax "<type>.default" for 
this:

     int zero = int.default;
or
     <any T> T zero() { return T.default; }

The default is already well-defined for existing types (primitives and 
refs); value types similarly need a default, which currently is defined 
to be the value with "all default" values for its fields.  (This is a 
delicate choice; on the one hand, this is the obvious implementation -- 
write zeros everywhere -- but for some values, this bit pattern might 
not always have an obvious meaning, and this puts a burden on 
implementation code to deal with this.)

Default values also implicitly arise from uninitialized fields and from 
newly allocated arrays.

On 12/31/2017 2:17 PM, Jack wrote:
> With generics soon to be expanded to also include primitive types, Value types, and data types, I'm under the impression that null (yes that elephant in the room that nobody likes to talk about) will be unsuitable as a default assignment for generic types. I'm not saying we need some sort of super null that encompasses all possible types, but how about a way to dynamically ask for the type-correct default value at run time?
> Maybe something like this: T foo = assignmentCompatibleInstanceForThisGenericType T;
>
> Thank you for your time,
> Jack



More information about the valhalla-spec-observers mailing list