custom attribute proposal (yeah, another one)
Timon Gehr
timon.gehr at gmx.ch
Fri Apr 6 06:53:59 PDT 2012
I think this proposal should be merged with Johannes' one.
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.
More information about the Digitalmars-d
mailing list