www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - equivalent of std.functional.partial for templates?

reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Aug 11 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven 
Schveighoffer wrote:
 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.
https://phobos.dpldocs.info/std.meta.ApplyLeft.html
Aug 11 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 11 August 2021 at 14:03:50 UTC, Paul Backus wrote:
 On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven 
 Schveighoffer wrote:
 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.
https://phobos.dpldocs.info/std.meta.ApplyLeft.html
Should have read further--this does not work with template functions due to [issue 1807.][1] My mistake. [1]: https://issues.dlang.org/show_bug.cgi?id=1807
Aug 11 2021
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Wednesday, 11 August 2021 at 14:08:59 UTC, Paul Backus wrote:
 On Wednesday, 11 August 2021 at 14:03:50 UTC, Paul Backus wrote:
 On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven 
 Schveighoffer wrote:
 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.
https://phobos.dpldocs.info/std.meta.ApplyLeft.html
Should have read further--this does not work with template functions due to [issue 1807.][1] My mistake. [1]: https://issues.dlang.org/show_bug.cgi?id=1807
So first, I though ApplyLeft would work, but I convinced myself it was focused on applying the template to arguments individually, but that's just the example used. ApplyLeft is exactly what I'm looking for, but as you said (and as my example shows), it doesn't work in this case. It would be nice to have something that would work (or make ApplyLeft work via a compiler change). Thanks for the nudge! -Steve
Aug 11 2021
prev sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 11 August 2021 at 14:08:59 UTC, Paul Backus wrote:
 [snip]

 Should have read further--this does not work with template 
 functions due to [issue 1807.][1] My mistake.

 [1]: https://issues.dlang.org/show_bug.cgi?id=1807
Looks like that strengthens the case for moving forward with DIP1023 (or something more general).
Aug 11 2021