digitalmars.D - Re: Suggestion: Change precedence of 'new'
- Jason House <jason.james.house gmail.com> Apr 10 2008
- Robert Fraser <fraserofthenight gmail.com> Apr 10 2008
- Jason House <jason.james.house gmail.com> Apr 10 2008
- Bill Baxter <dnewsgroup billbaxter.com> Apr 10 2008
Bill Baxter Wrote:Robert Fraser wrote:Unknown W. Brackets wrote:I think the problem is in supporting this syntax: auto x = new package.module.Class;
The argument (as far as I can tell) is that it would be supported only if trailing parentheses were supplied, so that expression would have to be written as "new package.module.Class().propertyMethod;".
That makes sense. So the rule would be that 'new' munches all the dot-separated identifiers to its right till it hits something besides a dot or an identifier. Here's another one from actual D code ported from Java: (new class Runnable { public void run() { if (canvas.isDisposed()) return; render(); canvas.swapBuffers(); canvas.getDisplay().timerExec(15, this); } }).run(); In Java the parens around that whole mess aren't necessary. --bb
Why would anyone write code like that in D? First of all a delegate works just as well as a class with one member. Second, if this is simply run like a blocking function call, why not define a nested function and just call it? PS: I'm not trying to knock down your feature request. I just find the example strange. I'm sure there's more useful examples available and you just picked one at random.
Apr 10 2008
Jason House wrote:Bill Baxter Wrote:Robert Fraser wrote:Unknown W. Brackets wrote:I think the problem is in supporting this syntax: auto x = new package.module.Class;
if trailing parentheses were supplied, so that expression would have to be written as "new package.module.Class().propertyMethod;".
dot-separated identifiers to its right till it hits something besides a dot or an identifier. Here's another one from actual D code ported from Java: (new class Runnable { public void run() { if (canvas.isDisposed()) return; render(); canvas.swapBuffers(); canvas.getDisplay().timerExec(15, this); } }).run(); In Java the parens around that whole mess aren't necessary. --bb
Why would anyone write code like that in D? First of all a delegate works just as well as a class with one member. Second, if this is simply run like a blocking function call, why not define a nested function and just call it? PS: I'm not trying to knock down your feature request. I just find the example strange. I'm sure there's more useful examples available and you just picked one at random.
Okay, how about this (admittedly contrived) example? public abstract class A { public final int foo() { // do stuff bar(); // do more stuff & return something } protected abstract void bar(); } void baz() { int k = new class A { protected void bar() { // ... } }.foo(); }
Apr 10 2008
Robert Fraser Wrote:Jason House wrote:Bill Baxter Wrote:Robert Fraser wrote:Unknown W. Brackets wrote:I think the problem is in supporting this syntax: auto x = new package.module.Class;
if trailing parentheses were supplied, so that expression would have to be written as "new package.module.Class().propertyMethod;".
dot-separated identifiers to its right till it hits something besides a dot or an identifier. Here's another one from actual D code ported from Java: (new class Runnable { public void run() { if (canvas.isDisposed()) return; render(); canvas.swapBuffers(); canvas.getDisplay().timerExec(15, this); } }).run(); In Java the parens around that whole mess aren't necessary. --bb
Why would anyone write code like that in D? First of all a delegate works just as well as a class with one member. Second, if this is simply run like a blocking function call, why not define a nested function and just call it? PS: I'm not trying to knock down your feature request. I just find the example strange. I'm sure there's more useful examples available and you just picked one at random.
Okay, how about this (admittedly contrived) example? public abstract class A { public final int foo() { // do stuff bar(); // do more stuff & return something } protected abstract void bar(); } void baz() { int k = new class A { protected void bar() { // ... } }.foo(); }
Actually, I meant in the context of the original proposal new foo().bar
Apr 10 2008
Jason House wrote:Bill Baxter Wrote:Robert Fraser wrote:Unknown W. Brackets wrote:I think the problem is in supporting this syntax: auto x = new package.module.Class;
if trailing parentheses were supplied, so that expression would have to be written as "new package.module.Class().propertyMethod;".
dot-separated identifiers to its right till it hits something besides a dot or an identifier. Here's another one from actual D code ported from Java: (new class Runnable { public void run() { if (canvas.isDisposed()) return; render(); canvas.swapBuffers(); canvas.getDisplay().timerExec(15, this); } }).run(); In Java the parens around that whole mess aren't necessary. --bb
Why would anyone write code like that in D? First of all a delegate works just as well as a class with one member. Second, if this is simply run like a blocking function call, why not define a nested function and just call it? PS: I'm not trying to knock down your feature request. I just find the example strange. I'm sure there's more useful examples available and you just picked one at random.
Yeh, sorry. It wasn't meant to be a convincing argument about why the features is needed. It was just some SWT Java code I happened to be porting yesterday, so it was more just to say "here's another thing that works without parens in the Java way". Unless you're porting Java code, I hope no one does write D code like that. :-) But I have wanted to do things like new Widget(args).enabled = false; --bb
Apr 10 2008