Suggestion: Change precedence of 'new'
Bill Baxter
dnewsgroup at billbaxter.com
Thu Apr 10 16:10:04 PDT 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;
>>> 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.
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
More information about the Digitalmars-d
mailing list