On 6 April 2012 16:53, Timon Gehr <timon.gehr@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.