Source code annotations alla Java

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 20 10:18:29 PST 2011


On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob at me.com> wrote:

> On 2011-01-20 15:02, Steven Schveighoffer wrote:
>> On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj at nospam.com>  
>> wrote:
>>
>>> Not long ago the Java Language people introduced the idea of
>>> annotations together with an annotation processing tool (apt).
>>>
>>> Now perhaps the idea of source code annotations is not actually a Java
>>> invention per se, however for someone learning D is there any
>>> equivalent idiom [of Java annotations] in the D language?
>>
>> Haven't used Java since they added annotations, but I think they are
>> like C# attributes?
>>
>> In any case, D has an annotation syntax like:
>>
>> @property
>>
>> But I think at the moment, annotations have no custom ability. Only
>> compiler-defined annotations are allowed. This may change in the future,
>> but probably not short-term. FWIW, I think we need a much richer
>> runtime-reflection capability before we can use custom annotations to
>> any great effect.
>>
>> -Steve
>
> I would rather formulate it like: currently D has a syntax for keywords  
> that are similar to Java annotations.

I don't think it's the same thing.  Keywords are not allowed to be used  
anywhere else, even for things that would parse properly were they not  
keywords.  They are anchors for the parser to determine where it is.  In  
contrast, a compiler-defined annotation is parsed just the same as a  
custom one, it's just that the meaning is predefined.

For example, you can legally do:

int property;

without error, but this won't even get past the parsing stage:

int struct;

-Steve


More information about the Digitalmars-d-learn mailing list