Casting reference array to any-T array.

Vitaly Davidovich vitalyd at gmail.com
Wed Jan 7 21:28:27 UTC 2015


Timely response - I was just writing to confirm that "new T[]" construct
will be supported.  As a side note, is the plan to continue erasing T for
ref types or will the specializer ultimately create a non-erased T array
for them?

On Wed, Jan 7, 2015 at 4:24 PM, Brian Goetz <brian.goetz at oracle.com> wrote:

> Yes, this is definitely unsound (and probably we should help by failing at
> compile time rather than runtime.)
>
> To fix your code, use the "new T[n]" syntax that gives you an array of the
> erasure of T for ref T and primitive arrays for primitive T (and an
> unchecked warning.)
>
>
> On 1/7/2015 4:19 PM, Michael Barker wrote:
>
>> Hi,
>>
>> A small thing that tripped me up yesterday.  The following code:
>>
>> package github.mikeb01;
>>
>> public class C<any T>
>> {
>>      T[] values;
>>
>>      public C()
>>      {
>>          values = (T[]) new Object[0];
>>      }
>>
>>      public static void main(String[] args)
>>      {
>>          new C<int>();
>>      }
>> }
>>
>> Compiles without error, but fails a runtime with:
>>
>> Specializing github.mikeb01.C${0=I}; searching for github/mikeb01/C.class
>> (not found)
>> Specializing github.mikeb01.C${0=I}; searching for github/mikeb01/C.class
>> (found)
>> Exception in thread "main" java.lang.IllegalStateException: [I
>> at
>> valhalla.specializer.SignatureSpecializer.asClassName(
>> SignatureSpecializer.java:79)
>> at
>> valhalla.specializer.SignatureSpecializer$ForType.getDescAsClassName(
>> SignatureSpecializer.java:156)
>>
>> Should that be a compile time failure, with an invalid cast from a
>> reference array to any-T array?
>>
>> Mike.
>>
>>


More information about the valhalla-dev mailing list