[JSR308] Others possible syntaxes for type annotations

Eugene Kuleshov eu at javatx.org
Thu May 17 17:34:10 EDT 2007


Maxim Kizub wrote:
> For me the specification List<@Readonly T> looks like @Readonly is the 
> annotation of T.
  Annotation on T would look like List<T<@Readonly>>.

  To reduce confusion we could probably use postfix syntax like List<T 
@Readonly>. Interestingly, that annotattion on T in that case still 
going to look like List<T<@Readonly>>
> Nevertheless, it's also an option to vote, and others people may 
> prefer your variation more.
> The main idea in my case is to make the syntax like C++ templates, 
> which allow not only types, but values as well.
  Right. There is a similarity between type parameters and annotations 
on the types. So, this syntax has a good sight, not to say that it 
allows to reduce ambiguity, probably even more then syntax that Neal 
Gafter is planning to propose. :-)
> Annotations separated by comma in the type argument list looks like 
> additional parameters of the type.
> Alternative semicolon separator has to clearly separate annotations 
> from types.
> In your case (with leading annotations) I would insert a separator 
> (comma or semicolon) before the types, like
>
> Hashtable<@Readonly @Threadsafe; A, B> or Hashtable<@Readonly 
> @Threadsafe, A, B>
  I think that comma or other separator is not needed there. So, coma or 
other separator don't add anything useful and there is no separator used 
between annotations in existing Java grammar.
  Like you said, annotations are easy to distinguish from type 
parameters (in both prefix and postfix variants) because of the "@" and 
at the same time annotations don't really have specific order and don't 
belong to the list of type parameters, which is using comma to separate 
elements in that list.

  regards,
  Eugene

> Eugene Kuleshov wrote:
>>
>>  Interesting. It seems like there is no ambiguity, but such syntax 
>> look confusing. Mostly because of "," or ";" used to separate 
>> annotations. But the idea is still worth investigation.
>>
>>  I've tried to play with the following grammar change and like it 
>> more when type annotations are put before type arguments or 
>> parameters. There is still no ambiguity and optional annotations are 
>> reasonably separate from other type arguments. The only concern here 
>> is that such syntax is more verbose then syntax proposed by the 
>> currently implemented prototype.
>>
>> TypeArguments:
>>        <[Annotations] TypeArgument {, TypeArgument} >
>> TypeParameters:
>>        <[Annotations] TypeParameter {, TypeParameter} >
>>
>>  Here is how examples from JSR-308 document would look in such syntax:
>>
>>    Map<String<@NonNull>, List<@NonEmpty Document<@Readonly>>> files;
>>
>>    o.<String<@NonNull>>m("...");
>>
>>    class Folder<F extends File<@Existing>> { ... }
>>    Collection<? super File<@Existing>>
>>
>>    class UnmodifiableList<T> implements List<@Readonly T<@Readonly>> 
>> { ... }
>>
>>    void monitorTemperature() throws TemperatureException<@Critical> { 
>> ... }
>>
>>    myString = (String<@NonNull>) myObject;
>>
>>    boolean isNonNull = myString instanceof String<@NonNull>;
>>
>>    new List<@NonEmpty @Readonly String>(myNonEmptyStringSet)
>>
>>    new <@Interned String> MyObject()
>>
>>    public <@Readonly> int size() { ... }  // method receiver
>>
>>    Class<String<@NonNull>> c = String<@NonNull>.class;
>>
>>  regards,
>>  Eugene
>>
>>
>> Maxim Kizub wrote:
>>> I think, grammar changes may be specified as:
>>>
>>> TypeArguments:
>>>       < TypeOrAnnotArgument {, TypeOrAnnotArgument} >
>>>
>>> TypeOrAnnotArgument :
>>>       [TypeArgument | Annotation]
>>>
>>> or as
>>>
>>> TypeArguments:
>>>       < ? [TypeArgument {, TypeArgument}] ?[; Annotations] >
>>>
>>> I'm not familiar with this parser grammar notation, so I don't know 
>>> how to express that annotations go after type parameters.
>>> For JavaCC grammar would be:
>>>
>>> TypeArguments() : {}
>>> {
>>> "<"
>>>   TypeArgument() ( "," TypeArgument())* ( "," Annotation() )*
>>> |   Annotation() ( "," Annotation() )*
>>> ">"
>>> }
>>>
>>> or
>>>
>>> TypeArguments() : {}
>>> {
>>> "<"
>>>   TypeArgument() ( "," TypeArgument())* [ ";" Annotations() ]
>>> |   [ ";" ] Annotations()
>>> ">"
>>> }
>>>
>>> JavaCC grammar needs no lookahead, since annotations are clearly 
>>> distinct from type parameters (because they start with @ token)
>>>
>>> Map<Foo<Boo>, Map<String, Boo>> can be annotated as
>>>
>>> Map<Foo<Boo, @BooAnnot>, Map<String, Boo, @MapInnerAnnot>, @MapAnnot>
>>>
>>> or using semicolon as the separator
>>>
>>> Map<Foo<Boo; @FooAnnot>, Map<String, Boo; @MapInnerAnnot>; @MapAnnot>
>>>
>>> or annotating everything will be
>>>
>>> Map<Foo<Boo<@BooAnnot>; @FooBooAnnot>, Map<String<@StringAnnot>, 
>>> Boo<@BooAnnot>; @MapStrBooAnnot>; @MapFooBooMapStrBooAnnot>
>>>
>>
>>
>> _______________________________________________
>> JSR308 mailing list
>> JSR308 at lists.csail.mit.edu
>> https://lists.csail.mit.edu/mailman/listinfo/jsr308
>>
>
>
> _______________________________________________
> JSR308 mailing list
> JSR308 at lists.csail.mit.edu
> https://lists.csail.mit.edu/mailman/listinfo/jsr308




More information about the JSR308 mailing list