www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin template can't contain statements: workaround?

reply Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Why can't we allow mixin templates to contain statements, as is the case
for regular mixins?
Is there a workaround?

here's a dummy example:

template mixin Foo{
 //some statement, eg: 'return;'
}

void fun(){
  mixin Foo;
}

Note that I can do this with a regular mixin, but template mixins are
cleaner (esp in more complex examples).
Mar 14 2015
next sibling parent "Meta" <jared771 gmail.com> writes:
On Sunday, 15 March 2015 at 01:20:55 UTC, Timothee Cour wrote:
 Why can't we allow mixin templates to contain statements, as is 
 the case
 for regular mixins?
 Is there a workaround?

 here's a dummy example:

 template mixin Foo{
  //some statement, eg: 'return;'
 }

 void fun(){
   mixin Foo;
 }

 Note that I can do this with a regular mixin, but template 
 mixins are
 cleaner (esp in more complex examples).
Wouldn't the most basic explanation be that templates cannot contain statements, therefore mixin templates can't either?
Mar 14 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sat, 14 Mar 2015 18:20:47 -0700, Timothee Cour via Digitalmars-d-learn
wrote:

 Why can't we allow mixin templates to contain statements, as is the case
 for regular mixins?
 Is there a workaround?
=20
 here's a dummy example:
=20
 template mixin Foo{
  //some statement, eg: 'return;'
 }
=20
 void fun(){
   mixin Foo;
 }
=20
 Note that I can do this with a regular mixin, but template mixins are
 cleaner (esp in more complex examples).
template Foo(int a, string b) { import std.format : format; enum Foo =3D q{ { import std.conv : to; return %2$s+to!string(%1$s); } }.format(a, b.stringof); } positional args, syntax highlighting, usage like `mixin(Foo!(42, "z"));`. maybe this will help.=
Mar 15 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 15 Mar 2015 13:28:33 +0000, ketmar wrote:

 template Foo(int a, string b) {
   import std.format : format;
   enum Foo =3D q{
     {
       import std.conv : to; return %2$s+to!string(%1$s);
     }
   }.format(a, b.stringof);
 }
=20
 positional args, syntax highlighting, usage like `mixin(Foo!(42,
 "z"));`.
 maybe this will help.
ah, that `+`... ;-)=
Mar 15 2015