mixin troubles
Josh Stern
josh_usenet at phadd.net
Thu Oct 5 16:55:44 PDT 2006
On Fri, 06 Oct 2006 09:04:05 +1000, Derek Parnell wrote:
> On Thu, 05 Oct 2006 14:28:01 -0500, Josh Stern wrote:
>
>> 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):
>
> I think you have missed my point.
>
> I want to mixin the template multiple times, each time giving the resulting
> statement a different identifier so it can be used in a simple manner.
Yes, I missed the point that the special difficulty being addressed was
trying to avoid a name clash caused by multiple definitions. But even so,
mixins and template seem to interact in the way I would expect and I don't
see that alias's are really required. You have two good choices here -
either use the 'x' and 'y' extra level of namespace idea...
import std.stdio;
template FFoo(alias b) {
typeof(b) foo() { return b; } // foo function
}
void main() {
int a=5;
double b = 3.14;
// FIRST TIME
mixin .FFoo!(a) x; // bring in the statement in the 'x' sub-namespace
writefln(x.foo());
// SECOND TIME
mixin .FFoo!(b) y; // bring it in in the 'y' sub-namespace.
writefln(y.foo());
}
...or just use an extra
level of braces around each distinct instantiation...
{
//First time
mixin .FFoo!(a);
}
{
// Second time
mixin .FFoo!(b);
writefln( foo());
}
> For
> example, using your 'different' name idea ...
>
> import std.stdio;
> template FFoo(alias b) {
> typeof(b) foo() { return b; } // foo function
> }
> void main() {
> int a=5;
> double b = 3.14;
> // FIRST TIME
> mixin .FFoo!(a) x; // bring in the statement calling it 'x' writefln(
> x() );
> // SECONND TIME
> mixin .FFoo!(b) y; // bring it in again but call it 'y' this time.
> writefln( y() );
> }
> }
> }
> This still fails and yet looks intuitive (to me at least).
I think of mixin as a smart replacement for macros so I wouldn't expect
the above to work.
More information about the Digitalmars-d-learn
mailing list