digitalmars.D - Mixin a constructor ?
- Jacob Carlborg (24/24) Sep 19 2009 Is it supposed to possible to mixin a constructor? The code below
- dsimcha (16/37) Sep 19 2009 I'm not sure exactly why this example doesn't work, but it looks like a ...
- Christopher Wright (19/23) Sep 19 2009 A template mixin introduces a new scope. This is pretty annoying in some...
- Jacob Carlborg (3/26) Sep 19 2009 The problem only seems to be when I'm having constructors both in the
- BCS (4/25) Sep 19 2009 IIRC mixins can't overload with other mixins or non mixins so if you wer...
- language_fan (4/21) Sep 19 2009 Since the constructor has no meaning outside classes, should it be
- Michel Fortin (8/12) Sep 20 2009 Personally, I'd like it very much if functions from template mixins
- Christopher Wright (3/13) Sep 20 2009 Also if you could implement a function from an interface with a template...
- Ellery Newcomer (4/8) Sep 20 2009 Checking whether a constructor is inside a class happens during one of
Is it supposed to possible to mixin a constructor? The code below doesn't compile. The error: is "main.d(23): Error: constructor main.A.this() does not match parameter types (int) main.d(23): Error: expected 0 arguments, not 1" template C () { this (int i) { } } class A { mixin C; this () { } } void main () { auto a = new A(3); }
Sep 19 2009
== Quote from Jacob Carlborg (doob me.com)'s articleIs it supposed to possible to mixin a constructor? The code below doesn't compile. The error: is "main.d(23): Error: constructor main.A.this() does not match parameter types (int) main.d(23): Error: expected 0 arguments, not 1" template C () { this (int i) { } } class A { mixin C; this () { } } void main () { auto a = new A(3); }I'm not sure exactly why this example doesn't work, but it looks like a bug. Please file. If you just want to insert a bunch of boilerplate without any parameters, you probably should try a string mixin instead. The following does work: enum string C = q{ this (int i) { } }; class A { mixin(C); this () { } } void main () { auto a = new A(3); }
Sep 19 2009
Jacob Carlborg wrote:Is it supposed to possible to mixin a constructor? The code below doesn't compile. The error: is "main.d(23): Error: constructor main.A.this() does not match parameter types (int) main.d(23): Error: expected 0 arguments, not 1"A template mixin introduces a new scope. This is pretty annoying in some cases. More often, it is convenient. Since the constructor is not in the same scope as the class, it might be causing problems. If you didn't introduce a new scope by default, it'd be easy enough to add it in the template or around the mixin: template MixMeIn() { int imInUrScope; { int imOuttaUrScope; } } Additionally, the difference would be easily detectable, because symbol collision would cause the code to fail to compile. Unless the mixin adds a function overload and you pass the address of the overload set somewhere, and now you're passing the wrong overload...which is an old problem for D, aggravated by D's property syntax, and unlikely to be fixed soon.
Sep 19 2009
On 9/19/09 20:55, Christopher Wright wrote:Jacob Carlborg wrote:The problem only seems to be when I'm having constructors both in the template and in the class.Is it supposed to possible to mixin a constructor? The code below doesn't compile. The error: is "main.d(23): Error: constructor main.A.this() does not match parameter types (int) main.d(23): Error: expected 0 arguments, not 1"A template mixin introduces a new scope. This is pretty annoying in some cases. More often, it is convenient. Since the constructor is not in the same scope as the class, it might be causing problems.If you didn't introduce a new scope by default, it'd be easy enough to add it in the template or around the mixin: template MixMeIn() { int imInUrScope; { int imOuttaUrScope; } } Additionally, the difference would be easily detectable, because symbol collision would cause the code to fail to compile. Unless the mixin adds a function overload and you pass the address of the overload set somewhere, and now you're passing the wrong overload...which is an old problem for D, aggravated by D's property syntax, and unlikely to be fixed soon.
Sep 19 2009
Hello Jacob,Is it supposed to possible to mixin a constructor? The code below doesn't compile. The error: is "main.d(23): Error: constructor main.A.this() does not match parameter types (int) main.d(23): Error: expected 0 arguments, not 1"IIRC mixins can't overload with other mixins or non mixins so if you were to drop the this() that should work. OTOH I'd be surprised if you can do that without breaking something else in the general case.template C () { this (int i) { } } class A { mixin C; this () { } } void main () { auto a = new A(3); }
Sep 19 2009
Since the constructor has no meaning outside classes, should it be interpreted as a free function if mixed in a non-class context? I really wonder how this could be valid code. Does the grammar even support the 3rd line?template C () { this (int i) { } } class A { mixin C; this () { } } void main () { auto a = new A(3); }
Sep 19 2009
On 2009-09-19 21:17:36 -0400, language_fan <foo bar.com.invalid> said:Since the constructor has no meaning outside classes, should it be interpreted as a free function if mixed in a non-class context? I really wonder how this could be valid code. Does the grammar even support the 3rd line?Personally, I'd like it very much if functions from template mixins could overload with functions from outside the mixin. It'd allow me to replace string mixins with template mixins in quite a few places. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Sep 20 2009
Michel Fortin wrote:On 2009-09-19 21:17:36 -0400, language_fan <foo bar.com.invalid> said:Also if you could implement a function from an interface with a template mixin.Since the constructor has no meaning outside classes, should it be interpreted as a free function if mixed in a non-class context? I really wonder how this could be valid code. Does the grammar even support the 3rd line?Personally, I'd like it very much if functions from template mixins could overload with functions from outside the mixin. It'd allow me to replace string mixins with template mixins in quite a few places.
Sep 20 2009
language_fan wrote:Since the constructor has no meaning outside classes, should it be interpreted as a free function if mixed in a non-class context? I really wonder how this could be valid code. Does the grammar even support the 3rd line?Checking whether a constructor is inside a class happens during one of the semantic passes. The parser makes no distinction between class/interface/template/struct/union bodies.
Sep 20 2009