mixin troubles

Josh Stern josh_usenet at phadd.net
Thu Oct 5 12:28:01 PDT 2006



I think the double use of "Foo" for the template namespace and the
function itself made the example confusing.  The following works
and makes the role of the template and the function and/or class
names clearer (no alias really needed):


import std.stdio;

template FFoo(alias b) {
	
  typeof(b) foo() { return b; } // foo function

  class FooClass {  
	typeof(b) opCall() { return b; } // operator() in FooClass
    }
}

void main() {

int a=5;

mixin .FFoo!(a);  // instantiates foo() and FooClass using local a as parameter	
int y = foo();
writefln(y);

FooClass obj = new FooClass;
y = obj();	
writefln(y);
	
}





On Wed, 04 Oct 2006 17:02:15 +1000, Derek Parnell wrote:

> On Wed, 04 Oct 2006 01:55:45 -0500, Chris Nicholson-Sauls wrote:
> 
>> Derek Parnell wrote:
>>> On Wed, 4 Oct 2006 00:03:17 -0400, Jarrett Billingsley wrote:
>>> 
>>>>"Derek Parnell" <derek at nomail.afraid.org> wrote in message 
>>>>news:327lkdzeqky1$.11w3vhc3ana9f.dlg at 40tude.net...
>>>>
>>>>
>>>>>Anyhow, what am I doing wrong here ...
>>>>>
>>>>>template Foo(alias b)
>>>>>{
>>>>>   typeof(b) Foo() { return b; }
>>>>>}
>>>>>
>>>>>void main()
>>>>>{
>>>>>   int a = 3;
>>>>>   double b = 3.14;
>>>>>   mixin Foo!(a) y;
>>>>>   mixin Foo!(b) z;
>>>>>   assert(y() == 3);
>>>>>   assert(z() == 3.14);
>>>>>}
>>>>>
>>>>
>>>>Mixins bug me too.  Here's the "correct" version of your code:
>>> 
>>> Thanks for patience.
>>>  
>>> 
>>>> template Foo(alias b)
>>>> {
>>>>    typeof(b) Foo() { return b; }
>>>> }
>>>>
>>>> void main()
>>>> {
>>>>    int a = 3;
>>>>    double b = 3.14;
>>>>    mixin .Foo!(a) y;
>>>>    mixin .Foo!(b) z;
>>>>    assert(y.Foo() == 3);
>>>>    assert(z.Foo() == 3.14);
>>>> }
>>>>
>>>>Notice the global scope operators on the mixin instantiations, and the 
>>>>explicit access of y.Foo/z.Foo.
>>>>
>> 
>> I may be wrong, but I think you could do this:
>> # mixin .Foo!(a).Foo  y ;
>> # mixin .Foo!(b).Foo  z ;
>> # assert(y() == 3);
>> # assert(z() == 3.14);
>> 
> 
> No such luck ...
> 
> test.d(10): template instance cannot use local 'a' as template parameter
> test.d(3): function test.Foo!(a).Foo cannot access frame of function main
> test.d(10): template instance test.Foo!(a) error instantiating
> test.d(10): mixin Foo!() is not defined




More information about the Digitalmars-d-learn mailing list