[lworld] What is the default of a Primitive.ref variable?

Jesper Steen Møller jesper at selskabet.org
Fri Jul 23 13:07:53 UTC 2021


Thanks, Srikanth - but...

... I *did* ask the compiler, but in the context of parameterized types...

public primitive class RefDefault<T> {
    public int i = 1;

    public static void main(String [] args) {
    	RefDefault<String>.ref l = RefDefault.ref.default;
    	System.out.println(l.i);
    }
}

This gave me a syntax error at the dot in for "RefDefault<String>.ref". 

 error: improperly formed type, some parameters are missing
    	RefDefault<String>.ref l = RefDefault.ref.default;
    	                  ^

The compiler will accept RefDefault.ref<String> but that looks off to me. It that the correct form of a reference projection of RefDefault<String>? Or should I fix that in the parser?

It turns out that eliding the type from RefDefault.ref.default (as in RefDefault.ref<String> l = RefDefault.ref.default)ill give a reference to a default value. I'll fix that in bug 8210906.

-Jesper

> On 23 Jul 2021, at 13.47, Srikanth Adayapalam <srikanth.adayapalam at oracle.com> wrote:
> 
> You could have asked the compiler ? 🙂
> 
> Prim.ref.default would have the same value and type that an uninitialized field of type Prim.ref would have by default.
> Alternately if you do new Prim.ref[10]  the cells of the newly constructed array would have same value and type as
> that of Prim.ref.default - so null.
> 
> As a result, the program you cite, should fail with NPE and it does on branch tip.
> 
> Srikanth  
> From: valhalla-dev <valhalla-dev-retn at openjdk.java.net> on behalf of Jesper Steen Møller <jesper at selskabet.org>
> Sent: 23 July 2021 16:17
> To: valhalla-dev <valhalla-dev at openjdk.java.net>
> Subject: [lworld] What is the default of a Primitive.ref variable?
>  
> Hi list
> 
> While making a test case for bug 8210906 (making SomeClass.default a poly-expression if SomeClass is parameterized), I came across the use of .default on a reference, as in
> 
> primitive class Prim {
>     int i = 42;
> 
>     public static void main(String [] args) {
>         Prim.ref p = Prim.ref.default; // Does this make sense at all?
>         System.out.println(p.i); // Should this NPE or print 0?
>     }
> }
> 
> Prim.ref is the reference projection of Prim, and is nullable, and is reference type. The default of a reference is null.
> But what is Prim.ref.default? Is it even a thing? Should it be disallowed?
> 
> -Jesper




More information about the valhalla-dev mailing list