www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Precedence of 'new' vs '.'

reply Frank Benoit <keinfarbton googlemail.com> writes:
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?
May 05 2009
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Frank,

 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?
 

vote++
May 05 2009
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
BCS wrote:
 Reply to Frank,
 
 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?

vote++

me too... Bugzilla, anyone?
May 05 2009
parent Frank Benoit <keinfarbton googlemail.com> writes:
Robert Fraser schrieb:
 me too... Bugzilla, anyone?

http://d.puremagic.com/issues/show_bug.cgi?id=2945
May 05 2009
prev sibling next sibling parent "Nick Sabalausky" <a a.a> writes:
"Frank Benoit" <keinfarbton googlemail.com> wrote in message 
news:gtqf6c$8ma$1 digitalmars.com...
 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'm torn on this. On one hand, I'm uncomfortable with having an operator that requires whitespaces bind tighter than an operator that isn't typically used with whitespace. To me, "new MyClass().run();" just *looks* like run() returns a type and you're trying to instantiate *that* type (with a default constructor). But on the other hand, I can certainly see the practiclity of the Java version and I've frequently had reason to use "(new MyClass()).run();" in my own code.
May 05 2009
prev sibling next sibling parent Derek Parnell <derek psych.ward> writes:
On Wed, 06 May 2009 00:39:12 +0200, Frank Benoit wrote:

 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?

So presumably if it was changed to behave as Java, to get the current functionality one would need to write ... new (MyClass().run()); -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
May 05 2009
prev sibling next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, May 5, 2009 at 8:49 PM, Derek Parnell <derek psych.ward> wrote:

 So presumably if it was changed to behave as Java, to get the current
 functionality one would need to write ...

 new (MyClass().run());

Except that currently has no meaning, since you can't return a type from a function :P
May 05 2009
prev sibling next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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.
May 05 2009
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 May 2009 21:43:54 -0400, Ary Borenszweig <ary 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
May 06 2009
prev sibling parent reply Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
Frank Benoit Wrote:

 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?

Related problem: string str = "qwe rty uio"; string[] arr = str.split(" "); // works class A { string foo() { return "qwe rty uio"; } } A a = new A; a.foo.split(" "); // doesn't work
May 06 2009
parent "Nick Sabalausky" <a a.a> writes:
"Eldar Insafutdinov" <e.insafutdinov gmail.com> wrote in message 
news:gtrooc$2ff9$1 digitalmars.com...
 Related problem:

 string str = "qwe rty uio";
 string[] arr = str.split(" "); // works

 class A {
    string foo() { return "qwe rty uio"; }
 }
 A a = new A;
 a.foo.split(" "); // doesn't work

Unless I misunderstand your point, that's already in bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=2883
May 06 2009