www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixin a constructor ?

reply Jacob Carlborg <doob me.com> writes:
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
next sibling parent dsimcha <dsimcha yahoo.com> writes:
== Quote from Jacob Carlborg (doob me.com)'s article
 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);
 }

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
prev sibling next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
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
parent Jacob Carlborg <doob me.com> writes:
On 9/19/09 20:55, Christopher Wright wrote:
 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.

The problem only seems to be when I'm having constructors both in the template and in the class.
 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
prev sibling next sibling parent reply BCS <none anon.com> writes:
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
next sibling parent reply Michel Fortin <michel.fortin michelf.com> writes:
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
parent Christopher Wright <dhasenan gmail.com> writes:
Michel Fortin wrote:
 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.

Also if you could implement a function from an interface with a template mixin.
Sep 20 2009
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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
prev sibling parent language_fan <foo bar.com.invalid> writes:
 template C ()
 {
 this (int i)
 {
 }
 }
 class A
 {
 mixin C;
 this ()
 {
 }
 }
 void main ()
 {
 auto a = new A(3);
 }


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?
Sep 19 2009