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

Petar Petar
Wed Dec 8 22:26:31 UTC 2021


On Wednesday, 8 December 2021 at 17:05:49 UTC, Timon Gehr wrote:
> On 12/8/21 9:07 AM, Petar Kirov [ZombineDev] wrote:
>> [...]
>
> Nice, so the error message is lying.

Closure support deserves way more love in the compiler. I'm quite 
surprised that that hack worked, given that various very similar 
rearrangements that I tried before didn't.

> This is a bit more complete:
>
> ```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;
> }
>
> // [snip]
>
> ```

Thanks, I was struggling to find a good name for this building 
block. `partiallyApply` is a natural fit. Also thanks for the 
move / forwarding icing.


More information about the Digitalmars-d-learn mailing list