Suggestion: common & polish constructors (+ destructors)
Kristian Kilpi
kjkilpi at gmail.com
Fri Nov 3 02:07:48 PST 2006
It would be nice if you could add code into constructors (and destructors)
with mixins. For example, you could initialize member objects/structures
added by a mixin, etc.
One way to achieve this is to have 'common constructors' that are executed
before the normal ones. For example:
class Foo {
common_this() {
a = 1;
}
this() {
b = 1;
}
this(int val) {
b = val;
}
}
Which would be equal to:
class Foo {
this() {
a = 1;
b = 1;
}
this(int val) {
a = 1;
b = val;
}
}
To be useful with mixins, multiple common operators should be allowed:
common_this() {
a = 1;
}
common_this() {
aa = 1;
}
Would be equal to:
common_this() {
a = 1;
aa = 1;
}
Each mixin could then have its own common constructor.
Actually I think this feature would be nice to have even if not used with
mixins. It would modulate constructors making them clearer and easier to
maintain.
Of course, similarly there could also be polish constructors (e.g.
'polish_this()') that are called after the normal ones.
And finally, there should also be common destructors (e.g.
'~common_this()', 'finish_this()', 'destroy_this()') to be used with
mixins. This way each mixin could add code to the actual destructor.
More information about the Digitalmars-d
mailing list