Jump to page: 1 2
Thread overview
Precedence of 'new' vs '.'
May 05, 2009
Frank Benoit
May 05, 2009
BCS
May 05, 2009
Robert Fraser
May 06, 2009
Frank Benoit
May 05, 2009
Nick Sabalausky
May 06, 2009
Derek Parnell
May 06, 2009
Ary Borenszweig
May 06, 2009
Eldar Insafutdinov
May 06, 2009
Nick Sabalausky
May 05, 2009
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
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
"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
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 06, 2009
Robert Fraser schrieb:
> me too... Bugzilla, anyone?

http://d.puremagic.com/issues/show_bug.cgi?id=2945
May 06, 2009
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 06, 2009
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 06, 2009
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 06, 2009
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
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
« First   ‹ Prev
1 2