Three Unlikely Successful Features of D
F i L
witte2008 at gmail.com
Wed Mar 21 08:56:42 PDT 2012
Another feature I like a lot about D, is it's approach to nested
classes. I'm not sure how it compares to other languages, but in
C# nested classes can be instanced individually and therefor
don't have access to their containing class's variables. D's
approach is much more logical, and works great for simple state
systems:
abstract class Actor {
interface State {
void update();
}
State state;
final void update() {
assert(state, "State is null");
state.update();
}
}
class Fighter : Actor {
Idle : State {
void update() {
punch(); // can punch automatically
}
}
Idle idleState = new Idle();
this() {
state = idleState;
}
void punch() { ... }
}
In C# I'd have to manually pass an Fighter reference to Idle's
constructor and manually manage the reference. It's a small
thing, but considering referencing the container class is a core
mechanic of any Stated object, it's a pain having to rewrite it,
while D just works.
More information about the Digitalmars-d
mailing list