equivalent of std.functional.partial for templates?

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 11 14:00:33 UTC 2021


I have a template function like this:

```d
auto foo(T, Args...)(Args args) {...}
```

If I try to bind the T only, and produce a partial template 
function which can accept any number of parameters, but has T 
already specified, I get an error, because instantiating `foo!T` 
means Args is length 0.

I was surprised not to see something in std.meta that could 
create this for me. In `std.functional` there is `partial` (which 
bizarrely only allows one parameter to be specified), but I don't 
see an equivalent for template parameters.

For functions, it seems IFTI can't see through to infer the 
template parameters of the alias, but you can have a function 
wrapper:

```d
template bindFirst(alias F, Args...)
{
    // this doesn't work
    // alias bindFirst(Args2...) = F!(Args, Args2);
    // but this does
    auto bindFirst(Args2...)(Args2 args2) { return 
F!(Args)(args2); }
}

alias bf = bindFirst!(foo, int);
bf("hello", 5); // ok
```

It would be nice if the alias worked, but I don't know what's 
involved to make IFTI work there. I know I can alter foo to be a 
template within a template. Perhaps that's the right answer. 
However, this doesn't help for functions already written that I 
can't alter.

Any ideas on how to solve this? I know my function solution is 
not very flexible...

-Steve


More information about the Digitalmars-d-learn mailing list