Suggestion: Change precedence of 'new'

Georg Wrede georg at nospam.org
Thu Apr 10 11:12:20 PDT 2008


Bill Baxter wrote:
> Georg Wrede 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.
>>
>>
>> Just to clarify, how would that look with the proposed precedence?
> 
> 
> You mean what does it look like in java?  Like this:
> 
>      new class Runnable {
>          public void run() {
>              if (canvas.isDisposed()) return;
>              render();
>              canvas.swapBuffers();
>              canvas.getDisplay().timerExec(15, this);
>          }
>      }.run();
> 
> (it's creating an anonymous subclass of 'Runnable', and running it.)

So, you gain the omission of one pair of parentheses, but lose in 
expressional clarity. In the current version, it is very clear to the 
reader (even to the one not familiar with the particular usage) what is 
going on. With the proposal, one has to really think hard, if one is not 
familiar with it from before. That's always a bad sign.

I'm not absolutely against this, but some more compelling examples would 
go a long way.



More information about the Digitalmars-d mailing list