is there any reason UFCS can't be used with 'new'?
    Meta via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sun Sep 28 13:30:41 PDT 2014
    
    
  
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);
}
    
    
More information about the Digitalmars-d-learn
mailing list