Any workaround for "closures are not yet supported in CTFE"?

Ali Çehreli acehreli at yahoo.com
Tue Dec 7 18:50:04 UTC 2021


On 12/7/21 7:30 AM, Andrey Zherikov wrote:

 > auto createDelegate(string s)
 > {
 >      return { s.writeln; };       // Error: closures are not yet
 > supported in CTFE
 > }

I don't know whether the workaround works with your program but that 
delegate is the equivalent of the following struct (the struct should be 
faster because there is no dynamic context allocation). Note the type of 
'dg' is changed accordingly:

struct FunctionObject {
   string s;

   this(string s) {
     this.s = s;
   }

   auto opCall() {
     import std.stdio : writeln;
     s.writeln;
   }
}

struct A
{
     FunctionObject[] dg;
}

auto createDelegate(string s) {
   return FunctionObject(s);
}

Ali



More information about the Digitalmars-d-learn mailing list