Jump to page: 1 2
Thread overview
is there any reason UFCS can't be used with 'new'?
Sep 28, 2014
Jay
Sep 28, 2014
Foo
Sep 28, 2014
Jay
Sep 28, 2014
Idan Arye
Sep 28, 2014
Jay
Sep 28, 2014
Jay
Sep 30, 2014
Nordlöw
Sep 30, 2014
Ali Çehreli
Sep 30, 2014
Ali Çehreli
Nov 10, 2014
Suliman
Nov 10, 2014
ketmar
Nov 10, 2014
Justin Whear
Nov 10, 2014
Ali Çehreli
Nov 10, 2014
Suliman
Nov 10, 2014
Ali Çehreli
Sep 28, 2014
Meta
Sep 28, 2014
Jay
Sep 28, 2014
Meta
Sep 29, 2014
Jay
September 28, 2014
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.
September 28, 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.

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);
}
September 28, 2014
thanks! but i'm still interested *why* you can't have this with 'new'. if there's no good reason i will file a bug report.

On Sunday, 28 September 2014 at 19:19:56 UTC, Foo wrote:
> 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);
> }

September 28, 2014
On Sunday, 28 September 2014 at 19:32:11 UTC, Jay wrote:
> thanks! but i'm still interested *why* you can't have this with 'new'. if there's no good reason i will file a bug report.

Because `new` is not a function - it's an operator.
September 28, 2014
On Sunday, 28 September 2014 at 19:41:29 UTC, Idan Arye wrote:
> Because `new` is not a function - it's an operator.

do you think the function call syntax has any chance to be implemented? is it just me who needs it?
September 28, 2014
On Sunday, 28 September 2014 at 19:19:56 UTC, Foo wrote:
> mixin template New(T) if (is(T == class)) {
> 	static T New(Args...)(Args args) {
>     	    return new T(args);
> 	}
> }

fwiw here's what i wrote:

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

...

New!Bar("hi").txt.writeln;

not as neat as your version but still improves on the ugly (new Class()).method syntax and doesn't need to be mixed into the class definition.
September 28, 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);
}
September 28, 2014
On Sunday, 28 September 2014 at 20:30:42 UTC, Meta wrote:
> 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);
> }

thanks! where should i put it in this table: http://wiki.dlang.org/Operator_precedence ?
September 28, 2014
On Sunday, 28 September 2014 at 20:50:07 UTC, Jay wrote:
> On Sunday, 28 September 2014 at 20:30:42 UTC, Meta wrote:
>> 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);
>> }
>
> thanks! where should i put it in this table: http://wiki.dlang.org/Operator_precedence ?

I'm not sure. Maybe it's on the same level as the Lambda Abstraction (14.5), but you'll probably have to do some testing to figure it out exactly.
September 29, 2014
On Sunday, 28 September 2014 at 22:17:03 UTC, Meta wrote:
> I'm not sure. Maybe it's on the same level as the Lambda Abstraction (14.5), but you'll probably have to do some testing to figure it out exactly.

precedence levels seem to be defined in `src/parse.h` (the `PREC` enum) and assigned to operators in `src/parse.c` (`initPrecedence()`).
« First   ‹ Prev
1 2