Proposal: user defined attributes

F i L witte2008 at gmail.com
Sun Mar 18 03:25:19 PDT 2012


F i L wrote:
> Plus, I don't know how you'd store anything but simple 
> variables, which more complex data would require a lot of 
> <entity>_variables.

I found a way:

mixin template Attribute(
   string type,
   string name,
   string attr,
   Params...)
{
     mixin (type ~ " " ~ name ~ ";");
     mixin (
         "auto " ~ name ~ "_" ~ attr ~ "()" ~
         "{ return new " ~ attr ~ "(Params); }"
     );
}

class Cool
{
     string s;
     this(string s)
     {
         this.s = s;
     }
}

class CoolClass
{
     mixin Attribute!("int", "a", "Cool", "Heh");
     mixin Attribute!("int", "b", "Cool", "Sup");
}

void main()
{
     auto c = new CoolClass();
     writeln(c.a, ", ", c.b); // 0, 0
     writeln(c.a_Cool().s); // Heh
     writeln(c.b_Cool().s); // Sup
}



More information about the Digitalmars-d mailing list