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.

The following code works perfectly fine. The precedence of the `new` operator was changed in a release a year or two ago.

class Button
{
        typeof(this) text(string t)
        {
                return this;
        }
        
        typeof(this) textColour(int c)
        {
                return this;
        }
}

void main()
{
        auto b = new Button()
                    .text("Hello, world!")
                    .textColour(0xFF000000);
}

Reply via email to