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

Andrey Zherikov andrey.zherikov at gmail.com
Thu Dec 9 04:14:35 UTC 2021


On Wednesday, 8 December 2021 at 17:05:49 UTC, Timon Gehr wrote:
> ```d
> import std.stdio, std.traits, core.lifetime;
> auto partiallyApply(alias fun,C...)(C context){
>     return &new class(move(context)){
>         C context;
>         this(C context) { foreach(i,ref c;this.context) 
> c=move(context[i]); }
>         auto opCall(ParameterTypeTuple!fun[context.length..$] 
> args) {
>             return fun(context,forward!args);
>         }
>     }.opCall;
> }
>
> alias Action = void delegate();
>
> Action createDelegate(string s){
>     import std.stdio;
>     return partiallyApply!((string str) => writeln(str))(s);
> }
>
> struct A{ Action[] dg; }
>
> A create(){
>     A a;
>     a.dg ~= createDelegate("hello");
>     a.dg ~= createDelegate("buy");
>     return a;
> }
>
> void main(){
>     enum a = create();
>     foreach(dg; a.dg)
>         dg();
> }
>
> ```

This is great, thanks you!


More information about the Digitalmars-d-learn mailing list