Proposal: user defined attributes

Adam D. Ruppe destructionator at gmail.com
Fri Mar 16 07:51:58 PDT 2012


On Friday, 16 March 2012 at 14:11:35 UTC, Manu wrote:
> Surely the term you're looking for here is @annotate(...) ?

Meh, I don't care that much about names. I went
with "note" anticipating people would complain about
@add_user_defined_attribute() being too long :)


> What if you want to annotate with a variable?

That *might* work because a variable is an expression too.
I'm not sure though. Will probably have to implement to
know for sure.

Of course, a constant can be represented as a struct
for name and value:

struct Description { string s; }

@note(Description("yada yada yada")) int a;


> Surely this is just as easy: @modulename.attribute int myThing;

Yeah, I think so. I remember this being a counterpoint
last time we talked about it, but I don't recall the
specific argument made.

> Perhaps that's the key distinction between 'annotation' and a 
> 'custom attributes' .. an annotation this way is a shared 
> compile time constant, associating with its type info, as you 
> suggest. A custom attribute is an instance-specific association 
> vontaining variable data.

Yeah, "annotation" might be the better word. That's
what I want here, but too late to change the subject name
now.


> attribute myAttribute

I think this could be done as a struct, but I haven't
used this kind of attribute at all so not sure...

But what I'm thinking is:

struct myAttribute(Type) {
    Type value;
    alias value this;

    bool bNeedsAttention;
    @property void refresh(bool bRefresh) { bNeedsAttention = 
bRefresh; }
}

myAttribute!int thing;
thing.refresh = true;

and thing can be substituted for an int anywhere else.


More information about the Digitalmars-d mailing list