mixin template can't contain statements: workaround?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 15 06:28:33 PDT 2015


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?
> 
> 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).

template Foo(int a, string b) {
  import std.format : format;
  enum Foo = 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150315/abdabe53/attachment.sig>


More information about the Digitalmars-d-learn mailing list