digitalmars.D - Suggestion: common & polish constructors (+ destructors)
- Kristian Kilpi (53/53) Nov 03 2006 It would be nice if you could add code into constructors (and destructor...
- Kristian Kilpi (4/5) Nov 03 2006 Hmm... ok, my name will now include my last name...
- Bill Baxter (23/31) Nov 03 2006 Actually what you suggest seems to already be the case for destructors.
- Kristian Kilpi (5/35) Nov 03 2006 Well now, that's nice. :)
- Georg Wrede (3/53) Nov 03 2006 I wonder if this is an accidental artifact of compiler internals?
- Kristian Kilpi (8/60) Nov 04 2006 If it is a bug, then it's a useful one.
- Lionello Lunesu (2/58) Nov 04 2006 It's mentioned in the changelog.
- Georg Wrede (2/68) Nov 04 2006 Cool!
It would be nice if you could add code into constructors (and destructor= s) = 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 execut= ed = before the normal ones. For example: class Foo { common_this() { a =3D 1; } this() { b =3D 1; } this(int val) { b =3D val; } } Which would be equal to: class Foo { this() { a =3D 1; b =3D 1; } this(int val) { a =3D 1; b =3D val; } } To be useful with mixins, multiple common operators should be allowed: common_this() { a =3D 1; } common_this() { aa =3D 1; } Would be equal to: common_this() { a =3D 1; aa =3D 1; } Each mixin could then have its own common constructor. Actually I think this feature would be nice to have even if not used wit= h = 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.
Nov 03 2006
On Fri, 03 Nov 2006 12:07:48 +0200, Kristian Kilpi <kjkilpi gmail.com> wrote:...Hmm... ok, my name will now include my last name... (Just wanted to tell you that you are dealing with the same guy here.)
Nov 03 2006
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
Nov 03 2006
On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter gmail.com> wrote:Kristian Kilpi wrote: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.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
Nov 03 2006
Kristian Kilpi wrote:On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter gmail.com> wrote:I wonder if this is an accidental artifact of compiler internals? I don't remember seeing any justifications for this behavior.Kristian Kilpi wrote: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.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
Nov 03 2006
On Fri, 03 Nov 2006 18:01:16 +0200, Georg Wrede <georg.wrede nospam.org> wrote:Kristian Kilpi wrote: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.On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter gmail.com> wrote:I wonder if this is an accidental artifact of compiler internals? I don't remember seeing any justifications for this behavior.Kristian Kilpi wrote: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.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
Nov 04 2006
Georg Wrede wrote:Kristian Kilpi wrote:It's mentioned in the changelog.On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter gmail.com> wrote:I wonder if this is an accidental artifact of compiler internals? I don't remember seeing any justifications for this behavior.Kristian Kilpi wrote: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.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
Nov 04 2006
Lionello Lunesu wrote:Georg Wrede wrote:Cool!Kristian Kilpi wrote:It's mentioned in the changelog.On Fri, 03 Nov 2006 14:33:20 +0200, Bill Baxter <wbaxter gmail.com> wrote:I wonder if this is an accidental artifact of compiler internals? I don't remember seeing any justifications for this behavior.Kristian Kilpi wrote: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.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
Nov 04 2006