天天看點

d預設類構造器

enum defaultClassConstructor = q{
    this(typeof(this.tupleof) params)
    {
        static foreach(i;0..this.tupleof.length)
        {
            this.tupleof[i] = params[i];
        }
    }
};

struct Color {}

class Point
{
    Color rgba;
    int x, y;
    bool hidden;

    mixin(defaultClassConstructor);
}

void main()
{
    Point p = new Point(Color(),123,456,false);
    assert(p.x == 123);
    assert(p.y == 456);
    assert(p.hidden == false);
}      
this(typeof(this.tupleof) params)
{
    this.tupleof = params; /* 工作 */
}