So, User-Defined Attributes

Philippe Sigaud philippe.sigaud at gmail.com
Fri Jan 4 07:45:20 PST 2013


OK, now that 2.061 is out, we have User-Defined Attributes (UDAs).

Except, there is no doc on them, no explanation, nothing. For 
people who did not follow the monstrous threads in November, it's 
as if UDA do not exist.

Myself, I upgraded from 2.060 only yesterday, and I never used 
them. Heck, I do not even know their syntax. This thread is for 
people like me, who wonder what UDA are, and what can be done 
with them.

Jacob Carlborg wrote documentation, but it's not merged into 
dlang.org yet, AFAICT. Here it is:

https://github.com/jacob-carlborg/d-programming-language.org/commit/bddbdf18353203ba12d8e0e44391e8b6a031b91a

Here is the executive summary:

User Defined Attributes (UDA) are compile time expressions that 
can be attached to a declaration. These attributes can then be 
queried, extracted, and manipulated at compile time. There is no 
runtime component to them.

Syntax:

@(3) int a;
@("string", 7) int b;

enum Foo;
@Foo int c;

struct Bar
{
     int x;
}

@Bar(3) int d;


 From that, I get we can put any CT symbol (even a value) as an 
attribute. I thought we were restricted to user defined types.

To query them, use __traits(getAttributes, symbol)


@('c') string s;
__traits(getAttributes, s)

this gives a tuple of attributes, that can be manipulated as a 
template tuple parameter (the 'raw' tuples).



Jacob, you doc says UDA are (grammatically) treated as storage 
classes. Can we do:

class C {
     @(3, "hello"):
         int a;
         double d;
}

And get both a and d with the same UDAs?
[Answer: silly me, I have 2.061. Yes, that works, both C.a and 
C.d have (3, "hello") as an attribute]

My own rapid reading tells me we will rapidly need good tuple 
manipulations templates in Phobos. We have mapping and filtering 
(I guess?), but overloading, discarding, dropping and folding 
will be needed also.




More information about the Digitalmars-d mailing list