I just took a look at the Expressions grammar for D 2.0 (NewExpression) and something caught my attention:
NewExpression:
NewArguments ClassArguments BaseClasslistopt { DeclDefs }
ClassArguments:
class ( ArgumentList ) class ( ) class
What exactly are these ClassArguments and DeclDefs? Is there an example somewhere that demonstrates their use?
ClassArguments is the keyword class
followed by the constructor arguments. DeclDefs are the declarations inside the class.
This syntax is to create an instance of an anonymous nested class, e.g.
import std.stdio;
void main() {
class K {
this() { writeln("K.__ctor"); }
}
auto f = new class (1, "4", 7.0) K {
this(int a, string b, double c) {
super();
writefln("anon.__ctor: %s %s %s", a, b, c);
}
};
}
(See http://ideone.com/cA1qo.)
The above can be rewritten into the less obscure form
import std.stdio;
void main() {
class K {
this() { writeln("K.__ctor"); }
}
class AnonymousClass : K {
this(int a, string b, double c) {
super();
writefln("anon.__ctor: %s %s %s", a, b, c);
}
}
auto f = new AnonymousClass(1, "4", 7.0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With