Suggestion: Change precedence of 'new'
Ary Borenszweig
ary at esperanto.org.ar
Fri Apr 11 09:46:16 PDT 2008
Bill Baxter escribió:
> Sometimes it's handy to invoke a function on a class right after
> creating it:
>
> new Thread(&func).run();
>
> Unfortunately that doesn't work in D right now. You have to put
> parentheses around the new expression because it has lower precedence
> than dotExpression:
>
> (new Thread(&func)).run();
It's just because you can ommit the parenthesis in case the constructor
(or any function) has no arguments, like
new Thread.run
Nice, huh? You save yourself a pair of parenthesis, but...
---
module one;
import std.stdio;
class One {
int foo;
this() {
foo = 1;
}
static class Two {
int foo;
this() {
foo = 2;
}
}
}
void main() {
auto x = new One.Two;
writefln("%s", x.foo); // 1 or 2? :-)
}
---
I really like how Java handles this: parenthesis are mandatory for
methods and constructors. Then you can have things like this:
class Foo {
int property:
void property(int p) {
property = p;
}
}
Foo.property --> the variable
Foo.property() --> the method
In C++ and D, you have to use _property, or fProperty, mProperty, or
some other ugly syntax. :(
More information about the Digitalmars-d
mailing list