View mode: basic / threaded / horizontal-split · Log in · Help
July 16, 2011
Why do template mixins have their own scope?
I guess this is the cause for  
http://d.puremagic.com/issues/show_bug.cgi?id=3332
It makes it practically impossible to mixin constructors because overload  
resolution doesn't work.
July 16, 2011
Re: Why do template mixins have their own scope?
Trass3r wrote:
> I guess this is the cause for
> http://d.puremagic.com/issues/show_bug.cgi?id=3332
> It makes it practically impossible to mixin constructors because overload
> resolution doesn't work.


mixin template foo(){
   immutable x = 1;
   int bar(){return x;}
}

immutable x = 2;

mixin foo!();

void main(){assert(bar == 1);}

Cheers,
-Timon
July 17, 2011
Re: Why do template mixins have their own scope?
Am 16.07.2011, 20:41 Uhr, schrieb Timon Gehr <timon.gehr@gmx.ch>:

> Trass3r wrote:
>> I guess this is the cause for
>> http://d.puremagic.com/issues/show_bug.cgi?id=3332
>> It makes it practically impossible to mixin constructors because  
>> overload
>> resolution doesn't work.
>
>
> mixin template foo(){
>     immutable x = 1;
>     int bar(){return x;}
> }
>
> immutable x = 2;
>
> mixin foo!();
>
> void main(){assert(bar == 1);}
>
> Cheers,
> -Timon
July 17, 2011
Re: Why do template mixins have their own scope?
I know but what is so bad about issuing an error message and you rename  
one of the two.
As I said it doesn't work out as soon as functions are involved.
July 17, 2011
Re: Why do template mixins have their own scope?
Trass3r wrote:
> I know but what is so bad about issuing an error message and you rename
> one of the two.
> As I said it doesn't work out as soon as functions are involved.

First of all, you would break some existing code for no benefit, including code
that mixes in two templates with clashing members.

Furthermore, it would be more difficult to implement, because templates already
come with their own scope.

Mixin templates are there to allow mixing in declarations without coping with the
problems of string mixins. An own scope is pretty much required for that.

Therefore, I think mixin templates having their own scope is sensible.

The bug fix is to just search for constructors in mixin scopes too.

Cheers,
-Timon
July 17, 2011
Re: Why do template mixins have their own scope?
On 2011-07-16 20:32, Trass3r wrote:
> I guess this is the cause for
> http://d.puremagic.com/issues/show_bug.cgi?id=3332
> It makes it practically impossible to mixin constructors because
> overload resolution doesn't work.

I've been knowing that template mixins behave like this for a long time. 
Don't know why I created that ticket, must have forgotten how mixins 
work. What is confusing is that the documentation says that a template 
mixin is just like copying the code and pasting it in the mixin place. 
Which it's not, shown by this ticket.

-- 
/Jacob Carlborg
July 17, 2011
Re: Why do template mixins have their own scope?
> The bug fix is to just search for constructors in mixin scopes too.

It isn't limited to constructors. Overload resolution for any function  
needs to be fixed.
Top | Discussion index | About this forum | D home