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
}

Reply via email to