Partial function application (Currying)

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sat Jan 20 20:58:49 UTC 2024


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
}
```


More information about the Digitalmars-d-learn mailing list