Can't use variadic arguments to functions that use templates

Ali Çehreli acehreli at yahoo.com
Tue Jul 23 10:17:38 PDT 2013


On 07/23/2013 09:22 AM, JS wrote:

 > On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote:
 >> On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote:
 >>> I don't think you understand(or I've already got confused)...
 >>>
 >>> I'm trying to use B has a mixin(I don't think I made this clear). I
 >>> can't use it as a normal function. e.g., I can't seem to do
 >>> mixin(B(t)). If I could, this would definitely solve my problem.
 >>
 >> I'll stick with the reduced example, maybe you can apply it to the
 >> real world:
 >>
 >>    template B(T...) {
 >>       string B(T b) {
 >>          string s;
 >>          foreach(i, Type; T)
 >>             s ~= Type.stringof ~ " " ~ b[i] ~ ";\n";
 >>          return s;
 >>       }
 >>    }
 >>
 >>    void main() {
 >>       enum forced = B("x", "a", "b");
 >>       pragma(msg, forced);
 >>       mixin(forced);
 >>    }
 >
 > What good does that do?

Makes a string, mixes it into the source code and then compiles it.

 > What if I want to use a run-time variable in the mix?

Just pass the runtime values to the template:

import std.stdio;

void main(string[] args) {
     if (args.length > 2) {
         writeln(B(args[1], args[2]));
     }
}

$ ./deneme abc xyz
string abc;
string xyz;

That is pretty amazing that the same function can be called at compile 
time and runtime.

Ali



More information about the Digitalmars-d-learn mailing list