Compile delegate with enum into proper function?

jmh530 john.michael.hall at gmail.com
Sat May 7 20:24:39 UTC 2022


On Saturday, 7 May 2022 at 18:46:03 UTC, Paul Backus wrote:
> On Saturday, 7 May 2022 at 18:36:40 UTC, jmh530 wrote:
>> In the code below, there is a two parameter function `foo` and 
>> an override of it with only one parameter. In the override 
>> case, I force the second one to be 1, but ideally there should 
>> be a way to specify it at compile-time.
>
> Have you tried [`std.functional.partial`][1]? Using it, your 
> example could be written like this:
>
> ```d
> import std.functional: partial;
>
> enum int a = 1;
> alias foo2 = partial!(foo, a);
> ```
>
> [snip]

Thanks. This is basically equivalent to

```d
int foo(int a)(int x) { return x + a; }
alias foo2 = foo!a;
```

The downside is that you wouldn't be able to `alias foo = foo!a`. 
Another approach would be to do something like

```d
int foo(int b = a)(int x) { return x + b; }
```

so that the default case could be handled.


More information about the Digitalmars-d-learn mailing list