Mixin Templates Over-restricted?

Diggory diggsey at googlemail.com
Sat May 25 11:28:08 PDT 2013


On Saturday, 25 May 2013 at 18:05:01 UTC, yaz wrote:
> Is there a reason for restricting mixin templates to only 
> include declarations?
> For example, the following code doesn't work. 
> (http://dpaste.dzfl.pl/1582a25e)
> Looking at the language specification 
> (http://dlang.org/template-mixin.html) this doesn't seem to be 
> an implementation limitation.
>
>
> import std.stdio;
>
> mixin template Test() {
>   writeln("Hello D People!");
> }
>
> void main() {
>   mixin Test;
> }
>
>
> I would have posted to the main newsgroup but I thought that 
> maybe I'm missing something.
>
> Thanks.

I think you can do it using a string mixin instead:
enum Test = `writeln("Hello D People!")`

void main() {
     mixin(Test);
}

The answer to your question is probably that D has to know the 
context for a template mixing at the point where it is declared 
rather than where it is used.

If non-declarations were allowed the semantic meaning of the 
template mixin would depend on the way it was used, and that's 
not allowed.

I could also be completely wrong of course :P


More information about the Digitalmars-d-learn mailing list