Proposal: user defined attributes

Manu turkeyman at gmail.com
Fri Mar 16 07:11:24 PDT 2012


On 16 March 2012 15:35, Adam D. Ruppe <destructionator at gmail.com> wrote:

> @note(Serializable.yes) int a;
>

Surely the term you're looking for here is @annotate(...) ?

This simple extension to the language enables libraries,
> using existing traits to navigate symbols, to get
> additional information about things and implement it
> however.
>
> The lack of user defined attributes is D's biggest missed
> opportunity right now, and I think this simple proposal
> will plug that hole.
>

I agree it's a very big hole against popular competing languages (Java, C#).

Previous user-defined attribute proposals have had counter
> arguments like these:
>
> 1) how do you define what is and is not a valid attribute?
>
> Here, the answer is pretty simple: you can put whatever notes
> you want on the thing. Whether the library uses it or not
> is up to it.
>

What if you want to annotate with a variable? Each instance may need to
hold some attribute state. This is extremely common, particularly in
serialisation systems which typically perform lazy updates, and need some
additional state for that.


> 2) OK, how do you namespace things so different libraries don't
> get different results?
>

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


> Am I missing anything?
>

 I think it only solves half the problem. There are certainly cases where
you just want to annotate with some sort of tag, so you can know to do
something special in this thing's case, but there are also times where you
want to associate some variable data with each instance.

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.
I'd suggest that @annotate(someEnum) could work effectively just as you
suggest, but it doesn't fill the requirement for custom attributes, which
could be implemented separately, something like:

attribute myAttribute
{
  this(int something);

  bool bNeedsAttention;

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

@myAttribute(10) int thing;

thing.refresh = true;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120316/672849df/attachment.html>


More information about the Digitalmars-d mailing list