www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why do template mixins have their own scope?

reply Trass3r <un known.com> writes:
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.
Jul 16 2011
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
Jul 16 2011
next sibling parent Trass3r <un known.com> writes:
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
Jul 16 2011
prev sibling parent reply Trass3r <un known.com> writes:
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.
Jul 16 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
Jul 17 2011
parent Trass3r <un known.com> writes:
 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.
Jul 17 2011
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Jul 17 2011