On Sunday, 28 September 2014 at 19:11:23 UTC, Jay wrote:
i want to chain 'new' with method calls on the created object. i found this on the internet:

window.mainWidget = (new Button()).text("Hello world"d).textColor(0xFF0000);

it would look much nicer with UFCS:

window.mainWidget = Button.new().text("Hello world"d).textColor(0xFF0000);

well, it's not *exactly* UFCS but you get what i mean.

mixin template New(T) if (is(T == class)) {
        static T New(Args...)(Args args) {
            return new T(args);
        }
}

class Bar {
        string txt;
        
        this() {
                txt = "Foo";
        }
        
        this(string t) {
                txt = t;
        }
        
        mixin New!Bar;
}

void main() {
        import std.stdio;
        
        writeln(Bar.New().txt);
        writeln(Bar.New("Bar").txt);
}

Reply via email to