Suggestion: common & polish constructors (+ destructors)
Kristian Kilpi
kjkilpi at gmail.com
Sat Nov 4 01:16:47 PST 2006
On Fri, 03 Nov 2006 18:01:16 +0200, Georg Wrede <georg.wrede at nospam.org>
wrote:
> Kristian Kilpi wrote:
>> On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter at gmail.com>
>> wrote:
>>
>>> Kristian Kilpi wrote:
>>>
>>>> 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.
>>>> [snip]
>>>> 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.
>>>
>>>
>>> Actually what you suggest seems to already be the case for
>>> destructors. Don't know if it's in the spec, but I just noticed it
>>> yesterday.
>>>
>>>
>>> template Death(int i)
>>> {
>>> ~this() {
>>> writefln("Death says: see you! ", i);
>>> }
>>> }
>>> class DClass
>>> {
>>> mixin Death!(1);
>>> mixin Death!(2);
>>> mixin Death!(3);
>>>
>>> ~this() {
>>> writefln("Class bye bye!");
>>> }
>>>
>>> mixin Death!(4);
>>> mixin Death!(5);
>>> mixin Death!(6);
>>> }
>>>
>>> The destructors get called in reverse order of appearance within
>>> DClass.
>>>
>>> --bb
>> Well now, that's nice. :)
>> This feature is not yet documented, maybe Walter is experimenting
>> things for future releases... Hopefully we'll have something like it
>> for the ctors also.
>
> I wonder if this is an accidental artifact of compiler internals?
>
> I don't remember seeing any justifications for this behavior.
If it is a bug, then it's a useful one.
Several times in my C++ projects I have used macros to add methods etc to
classes and code to constructors (and that's even if objects are value
types and structures have ctors etc in C++). It's a very annoying -- and
potentially buggish -- way. An alternate way would be to write/copy the
same code over and over again, but it's more error-prone, of course.
More information about the Digitalmars-d
mailing list