Custom attributes (again)
Steven Schveighoffer
schveiguy at yahoo.com
Fri Apr 6 05:58:20 PDT 2012
On Fri, 06 Apr 2012 03:54:15 -0400, Walter Bright
<newshound2 at digitalmars.com> wrote:
> On 4/6/2012 12:49 AM, Alex Rønne Petersen wrote:
>> What about type declarations? I think those ought to be supported too.
>> E.g. it
>> makes sense to mark an entire type as @attr(serializable) (or the
>> inverse).
>
>
> That would make it a "type constructor", not a storage class, which we
> talked about earlier in the thread. I refer you to that discussion.
I think there is a huge misunderstanding here.
A type constructor alters a type. Annotations are not type constructors.
for example:
class C {}
@foo class D {}
@foo C c;
@foo2 D d;
D d2 = d;
In all these cases, the @foo or @foo2 affects the *declaration*, not the
*type*. So:
1. D's type is not affected, it's still class D. But an annotation has
been stored on the *symbol* D, such that it can be looked up later.
2. annotations on variables do *not* affect the type of the variable. The
assignment to d2 works fine.
3. The following symbols have annotations attached to them:
C: none
D: @foo
c: @foo
d: @foo2
d2: none
Note that d and d2 have no annotations attached even though they are of
type D. Because the type isn't affected by annotations.
An annotation is simply metadata, stored in the compiler, and looked up
via compiler directives. Optionally (and I would encourage this),
TypeInfo would store annotations on type declarations for retrieval at
runtime.
-Steve
More information about the Digitalmars-d
mailing list