[type-annos-observers] ordering of annotated array brackets
Timo Kinnunen
timo.kinnunen at gmail.com
Wed Mar 5 03:24:08 PST 2014
Hi,
I can’t help thinking that none of what’s being discussed would actually matter if there was a syntactically legal way to write something like this and doing anything else was deemed ambiguous:
@A (T[]) arr1;
(@A T)[] arr2;
(more realistically perhaps @Length(10)<T[]> arr1 and <@NotNull T>[] arr2, in an alternate reality)
And then there’s the disquieting doubt about whether the syntax changes that would allow
@B ((@C ((@A T)[]))[]) arrList3; (more realistically @B <@C <@A T>[]>[] arrList3; )
might in the end have turned out smaller than and obviating the need for the syntax changes needed to allow
T @A[]
and
org.foo. @A T
both.
I know, I know, “it’s too late for Java 8”, it was too late last year when I first started paying attention and probably has been too late since after 2009 when lambda syntax was decided. Hopefully it’s not yet too late for Java 10.
--
Have a nice day,
Timo.
Sent from Windows Mail
From: Markus Keller
Sent: Wednesday, March 5, 2014 12:26
To: type-annotations-spec-observers at openjdk.java.net
Cc: alex.buckley at oracle.com
Alex Buckley <alex.buckley at oracle.com> wrote on 2014-03-05 00:13:37:
> I disagree where post-identifier brackets are concerned, because I think
> that adding them should not cause the reader to do gymnastics on the
> annotated array type. It's simply astonishing that post-identifier []
> pairs are inserted _before_ the normal [] pairs. I keep recalling the
> beauty of:
>
> @A T x;
> @A T[] y;
>
> whereby adding [] doesn't affect anything to the left of []; in that
> vein, adding @C [] should not affect anything to the left @B [].
>
> @A T @B [] arr1;
> @A T @B [] arr2 @C [];
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. Using a generic List "@B-List of
@C-List of @A T", a similar type would look like this:
@B List<@C List<@A T>> arrList3;
See how the order of annotations is @B, @C as well when @B is the
outermost type annotation?
[1]
http://types.cs.washington.edu/jsr308/specification/java-annotation-design.html#array-syntax
Markus
More information about the type-annotations-spec-observers
mailing list