mixin troubles

Josh Stern josh_usenet at phadd.net
Thu Oct 5 19:58:01 PDT 2006


On Fri, 06 Oct 2006 10:43:59 +1000, Derek Parnell wrote:

> On Thu, 05 Oct 2006 18:55:44 -0500, Josh Stern wrote:
> 
>>   mixin .FFoo!(a) x;  // bring in the statement in the 'x' sub-namespace
> 
> Thank you. This was the key information that I didn't pick up from the
> docs. It is documented but I didn't get it until now. The important passage
> from the docs is "A mixin has its own scope". I kept on reading that as
> "the mixin's template has its own scope" but now I understand it is the
> mixin statement itself that creates a new scope/namespace, and that the
> "MixinIdentifier" is the optional name one can give to that scope. Then one
> can use the MixinIdentifier as a namespace qualifier to disambiguate
> identifiers brought in by the mixin statements.
> 
> Mind you, this would have been made easier if the 'alias' argument could
> also have been used to 'set' the identifier declared in the template.

You can't use exactly the same name - i.e. mixin .FFoo!(a) a;
But you could adopt some convention of related names - i.e. 
mixin .FFoo!(a) for_a;
mixin .FFoo!(b) for_b;

for_a.foo();
for_b.foo();

etc.



> In other words it would have been 'smart' for mixin to allow ...
> 
>  template foo(alias a, alias b)
>  {
>     typeof(b) a(b) { . . . }
>  }
> 
>  mixin foo!(Bar, 3);
>  mixin foo!(Qwe, 3.14);
> 
> 
> to generate code as ...
> 
>     typeof(3) Bar(3) { . . . }
>     typeof(3.14) Qwe(3.14) { . . . }
> 
> then there would have been no disambiguation required.




More information about the Digitalmars-d-learn mailing list