Suggestion: Change precedence of 'new'
Robert Fraser
fraserofthenight at gmail.com
Thu Apr 10 12:38:24 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.
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();
}
More information about the Digitalmars-d
mailing list