So, User-Defined Attributes

Johannes Pfau nospam at example.com
Sat Jan 5 03:57:38 PST 2013


Am Fri, 04 Jan 2013 21:33:03 +0100
schrieb "Era Scarecrow" <rtcvb32 at yahoo.com>:

> On Friday, 4 January 2013 at 15:45:21 UTC, Philippe Sigaud wrote:
> > 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.
> 
>   This is sorta like tuples; But from the brief summaries I cannot 
> fully understand how or where they would be used. I understand 
> some attributes can be made and added that some compilers may use 
> (@noreturn as an example), but outside of the compiler I'd need 
> an example of how to make use of them.
>

One example is std.benchmark. It currently detects if a function is a
benchmark function by checking the name: void benchmark_stdio();

With UDAs we can do this:
@benchmark stdio();
or
@benchmark("Benchmarking stdio reads, using xKB buffer") benchStdio1();

There's still the issue of "How do I get all the declarations in a
module (/ the applications)", but UDAs already help a lot.


More information about the Digitalmars-d mailing list