Is there a plan to add support for closures in CTFE?

Salih Dincer salihdb at hotmail.com
Sat Aug 13 08:10:13 UTC 2022


On Saturday, 13 August 2022 at 06:30:13 UTC, Salih Dincer wrote:
> That works:
> ```d
>
>     enum s = f1.f3("text");
>
> ```
>

Sorry for my quick reply. When I tried it with my own codes, I 
understood what you meant better; during compile time you want to 
take a delegate and give the delegate...

```d
enum {
    f1 = "1: ", f2 = "2, ", f3 = "3, "
}
alias dstr = string delegate(string);

       auto fun1(dstr dg, string start)
       {
         return dg(f1 ~ start);
       }
       dstr fun2(dstr dg)
       {
         return str => dg(f2 ~ str);
       }
       dstr fun3()
       {
         return str => f3 ~ str;
       }

auto funS(string s) { return f2 ~ s; }
enum s = fun3.fun1("Start").funS;

void main()
{
     import std.stdio;
     s.writeln(": ", typeid(s));

     fun3.fun2.fun1("Start").writeln;
     writeln(fun1(fun2(fun3()), "Start"));
}
/* Prints:
2, 3, 1: Start: immutable(char)[]
3, 2, 1: Start
3, 2, 1: Start
*/
```

SDB at 79




More information about the Digitalmars-d mailing list