Re: Mixin a constructor ?

2009-09-20 Thread Christopher Wright

Michel Fortin wrote:

On 2009-09-19 21:17:36 -0400, language_fan  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.


Re: Mixin a constructor ?

2009-09-20 Thread Ellery Newcomer
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.


Re: Mixin a constructor ?

2009-09-20 Thread Michel Fortin

On 2009-09-19 21:17:36 -0400, language_fan  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.for...@michelf.com
http://michelf.com/



Re: Mixin a constructor ?

2009-09-19 Thread language_fan
>> 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?


Re: Mixin a constructor ?

2009-09-19 Thread Jacob Carlborg

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.




Re: Mixin a constructor ?

2009-09-19 Thread BCS

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);
}





Re: Mixin a constructor ?

2009-09-19 Thread Christopher Wright

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.


Re: Mixin a constructor ?

2009-09-19 Thread dsimcha
== Quote from Jacob Carlborg (d...@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);
}



Mixin a constructor ?

2009-09-19 Thread Jacob Carlborg
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);
}