Precedence of 'new' vs '.'

Steven Schveighoffer schveiguy at yahoo.com
Wed May 6 05:45:41 PDT 2009


On Tue, 05 May 2009 21:43:54 -0400, Ary Borenszweig <ary at esperanto.org.ar>  
wrote:

> Frank Benoit escribió:
>> In Java one can write:
>>  new MyClass().run();
>>  in D this does not compile, parenthesis are needed.
>>  (new MyClass()).run();
>>  But why is the D language designed like that?
>
> I think it's because the (already hated by many, including myself)  
> ability to invoke a function (or a constructor) without parenthesis.
>
> So:
>    new MyClass.run
> and
>    new MyClass().run
>
> should be the same, right?
>
> But in the first case MyClass could be a package name and you are  
> constructing a new "MyClass.run" instance. You could make them behave  
> differently, but some would say it's not consistent, etc.

If properties are every properly established, I hope the ability to call  
default constructors without having parens does not go away.  It's  
different than a property function call.

I would say it's ok to make them behave differently.  As others have said,  
we already have instances where you have to use parens to delineate where  
you are calling a function versus accessing a member.

For example, the (admittedly seldom used) chaining call of Stdout in  
Tango, if you want to insert a new line it's:

Stdout("hi")(4).newline()("hi");

Without the extra parens, it looks like you're trying to call newline with  
the argument "hi".

-Steve



More information about the Digitalmars-d mailing list