Is it possible to call a delegate at compile time?

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 22 21:52:07 PDT 2017


auto foo(Args...)(ref Args args)
{
     with (Module!"std.conv")
     with (Module!"std.stdio") {
         return () => {
             string[] s;
             foreach (i, arg; args) {
                 static if (is(Args[i] == string)) {
                     s ~= arg;
                 } else {
                     s ~= to!string(arg);
                 }
             }

             debug writeln(fmt());
             return cast(immutable)s;
         }();
     }
}

template Module(string name)
{
     mixin("import Module = " ~ name ~ ";");
}

void main()
{
     static immutable i = 7;
     static immutable s = "teen";

     static immutable res = foo(i, s)();
     writeln(res);
}

I desire to call foo() at compile...  As implemented it does not 
happen, but it's not immediately clear what I am missing. Or is 
this simply not possible as yet? What is the proper way to 
redesign this template so that it will execute at compile time?

Thanks,
Andrew


More information about the Digitalmars-d-learn mailing list