Custom attributes (again)

Timon Gehr timon.gehr at gmx.ch
Fri Apr 6 03:35:37 PDT 2012


On 04/06/2012 11:41 AM, Johannes Pfau wrote:
>
> Syntax in D would be different of course, but I see absolutely no need
> for the redundant (and ugly) @attr.
>
> 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.
>

This all seems very reasonable. I think this is how things should work. 
(maybe syntax will need to be different, because Walter does not seem to 
like the idea of un-reserving @identifier.)


More information about the Digitalmars-d mailing list