www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin Templates Over-restricted?

reply "yaz" <yazan.dabain gmail.com> writes:
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.
May 25 2013
parent reply "Diggory" <diggsey googlemail.com> writes:
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
May 25 2013
next sibling parent "yaz" <yazan.dabain gmail.com> writes:
On Saturday, 25 May 2013 at 18:28:09 UTC, Diggory wrote:
 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
The whole point of mixin templates is that they are evaluated within the scope where the mixin appears. Actually, an example of this effect (copied from the spec and modified slightly), where the semantic meaning of the template changes, compiles correctly. mixin template Foo() { auto test() { return y; } } void test1() { int y = 1; mixin Foo; assert(test() == 1); } void test2() { string y = "hello"; mixin Foo; assert(test() == "hello"); } void main() { test1(); test2(); }
May 25 2013
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 25 May 2013 at 18:28:09 UTC, Diggory wrote:
 D has to know the context for a template mixing at the point 
 where it is declared rather than where it is used.
Quite the opposite in fact. Templates exist in the context that they are defined in, mixin templates in the context where they are instantiated.
May 25 2013