DIP6: Attributes
Steven Schveighoffer
schveiguy at yahoo.com
Tue Aug 4 06:34:38 PDT 2009
On Tue, 04 Aug 2009 03:03:12 -0400, Don <nospam at nospam.com> wrote:
> A question: in C#/Java, can you have annotations on function pointer and
> delegate declarations?
>
> void foo( int delegate(int) pure dg) {
> ...
> }
> What would this look like with annotations?
>
> (The underlying question is, how do annotations interact with the type
> system?)
I'm not familiar with Java annotations, it's been a while since I used
Java.
In C#, you would not use an attribute as a type modifier, the attributes
are attached to a declaration (e.g. a class, member, function parameter).
So basically, an attribute can modify how the compiler treats something,
but it doesn't allow you to define new types with it. I suppose you could
use it that way, but I think it would be better to just use it as hints
and information, not an integral part of the type system.
When you have runtime reflection, you can query for attributes on almost
anything, but they are usually descriptors, and don't affect type
matching. The only places I've used it are as hints to other things
reading my compiled objects. For example, SerializableAttribute is used
by serializers to know what to serialize and what not to serialize.
If pure weren't part of the function signature, but simply a way to
compile a function, then it would be fine as an annotation.
A good usage for annotation, for example, would be scope, since it simply
indicates where the compiler should put things, and doesn't affect types
or signatures.
>> To answer Don's point, there is nothing saying that the compiler can't
>> read attributes and change its behavior. Of course, those would have
>> to be builtin attributes.
>
> Yes, but then they're just keywords, with an @ in front. You'd just be
> kidding yourself if you think you've reduced the keyword count.
keywords are global, that was one of Walter's main points against adding a
keyword for properties. Since annotations necessarily have a different
syntax than normal symbols, you don't have to worry about avoiding those
words. Others have also said that adding a builtin annotation would be
easier because it doesn't affect the parser.
For the record, I'm not really keen on using annotations for *everything*
as some people have suggested. They have good uses, but I don't think
it's worth adding them until we can have full reflection capabilities so
we can get at elements of code and therefore get the annotations
associated with it. I see much more usefulness for annotations as
reflection hints than as a replacement for keywords.
-Steve
More information about the Digitalmars-d
mailing list