custom attribute proposal (yeah, another one)

Manu turkeyman at gmail.com
Fri Apr 6 07:04:31 PDT 2012


On 6 April 2012 16:53, Timon Gehr <timon.gehr at gmx.ch> wrote:

> I think this proposal should be merged with Johannes' one.
>

I think Johannes proposal already nails it. What benefits would be merged
from this proposal? How would they influence the design?


On 04/06/2012 11:41 AM, Johannes Pfau wrote:
>
>> Declaring a custom attribute:
>> ---------
>> module std.something;
>>
>> struct Author
>> {
>>    string name;
>>    public this(string name)
>>    {
>>        this.name = name;
>>    }
>> }
>> ---------
>>
>> Using it:
>> ---------
>> import std.something; //Usual namespace lookup rules apply to attributes
>>
>> /*
>>  * @Author(param) calls the constructor of the Author struct and
>>  * attaches the struct instance to test. Probably @Author (without
>>  * parenthesis) coud be made to mean std.something.Author.init
>>  */
>> @Author("Johannes Pfau") int test;
>> ---------
>>
>> Attaching attributes multiple times as in C# should be possible.
>>
>> Using reflection to get that attribute:
>> ---------
>> if(__traits(hasAttribute, test, std.something.Author))
>> {
>>    Author[] authors = __traits(getAttribute, test,
>>        std.something.Author);
>> }
>> ---------
>>
>> An array is used here to support attaching the same attribute multiple
>> times. Of course "auto authors = ..." should be usable here too.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120406/7cdac798/attachment.html>


More information about the Digitalmars-d mailing list