Partial function application (Currying)
atzensepp
webwicht at web.de
Sun Jan 21 00:11:19 UTC 2024
On Saturday, 20 January 2024 at 20:58:49 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> On 21/01/2024 9:55 AM, atzensepp wrote:
>> import std.stdio; // Overloads are resolved when the partially
>> applied function is called // with the remaining arguments.
>> struct S { static char fun(int i, string s) { return s[i]; }
>> static int fun(int a, int b) { return a * b; } } void main() {
>> alias fun3 = partial!(S.fun, 3); writeln(fun3("hello")); //
>> 'l' writeln(fun3(10)); // 30 }
>
> This worked:
>
> ```d
> import std.functional;
> import std.stdio;
>
> // Overloads are resolved when the partially applied function
> is called
> // with the remaining arguments.
> struct S
> {
> static char fun(int i, string s)
> {
> return s[i];
> }
>
> static int fun(int a, int b)
> {
> return a * b;
> }
> }
>
> void main()
> {
> alias fun3 = partial!(S.fun, 3);
> writeln(fun3("hello")); // 'l'
> writeln(fun3(10)); // 30
> }
> ```
Hello Rikki,
thank you, this helps a lot although the polymorphism does not
work in my environment.
```
partial2.d:23:17: error: function std.functional.partial!(fun,
3).partial (string _param_0) is not callable using argument types
(int)
writeln(fun3(10)); // 30
```
But when I rename the functions for each case currying works well.
More information about the Digitalmars-d-learn
mailing list