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

Andrey Zherikov andrey.zherikov at gmail.com
Tue Dec 7 15:30:38 UTC 2021


I have a struct `A` that stores delegates. The problem is that 
I'm getting "`Error: closures are not yet supported in CTFE`" if 
I create an compile-time constant value of type `A`:

```d
struct A
{
     void delegate()[] dg;
}

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

A create()
{
     A a;
     a.dg ~= createDelegate("hello");
     a.dg ~= createDelegate("buy");
     return a;
}


void main()
{
     enum a = create();         // If change 'enum' to 'auto' then 
everything works
     foreach(dg; a.dg)
         dg();
}
```

How can I workaround this and have compile-time object with 
delegates?


More information about the Digitalmars-d-learn mailing list