[type-annos-observers] ordering of annotated array brackets

Alex Buckley alex.buckley at oracle.com
Wed Mar 5 11:34:20 PST 2014


On 3/5/2014 2:26 AM, Markus Keller wrote:
> Note that this is not just about post-identifier [] pairs. It's a direct
> consequence of the notation for multi-dimensional array types.
>
> If you have a declaration
>
>    @A T @B [] @C [] arr3;
>
> then arr3 is of type "@B-array of @C-array of @A T", and the component
> type of arr3 is
>
>    @A T @C [] arr3component = arr3[7];
>
> because "@B []" is the outermost dimension. This is a bit surprising, but
> as argued in [1], the surprise is that array brackets have to be written
> after the element type, and not before.

I agree with all of the above. The component type of arr3 is @A T @C [], 
sure. However, I don't think that has a bearing on the question at hand: 
why can't the component type of

   @A T @B [] arr4 @C [];

simply be decreed as @A T @C [] ?

Now, I know the answer; it's so that the annotations work out on:

   @A T @B [] arr4 @C [];
   @A T @B [] arr5;
   arr5 = arr4[0];
   arr4[0] = arr5;

But look, if I write:

   T @B [] @C [] arr7;
   T       @C [] arr8 @B [];

then no-one will be astonished to learn that arr7 and arr8 have 
different types from an annotation point of view: @B [] @C [] versus @C 
[] @B [].

Post-identifier brackets are a corner case, and mixed array syntax is a 
corner case of that corner case. It's unreasonable to bend a core part 
of the language - i.e. the rules for "declared type of a field / local 
var / formal parameter" - to support a corner case of a corner case.

(If you must, declare arr8 as:

   T arr8 @B[] @C [];

so it has the same annotated array type as arr7.)

Alex


More information about the type-annotations-spec-observers mailing list