Template currying
Ed via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 5 14:54:29 PDT 2016
On Thursday, 5 May 2016 at 20:17:08 UTC, Yuxuan Shui wrote:
> So I was working on a parser combinator library, where the
> combinators take parsers as template argument. It works well
> until recently I decided to change the parsers so they would
> take Ranges instead of just strings.
>
> The combinator used to look like:
>
> template Comb(alias parser1, alias parser2)
>
> Now it looks like
>
> template Comb(alias parser1, alias parser2, Range)
>
> And I want to have the compile deduce the Range template
> arguments, so I don't need to change the code that uses them.
> But I found out it's hard to partially instantiate templates.
>
> It's OK to do that at the call site, e.g. Comb!(a, b)(range)
> works without specifying the Range. But sometimes I want to
> alias the result parser so I can use it at multiple places, but
>
> alias new_parser = Comb!(a, b);
>
> doesn't work.
>
> I need either to define Comb differently:
>
> template Comb(alias p1, alias p2) {
> template Comb(Range) {
>
> or not use alias:
>
> template new_parser(alias p1, alias p2){
> alias new_parser(Range) = Comb!(p1, p2, Range);
> //Something like this, not tested
> }
>
> I think it would be really nice if alias can be automatically
> translated to the latter one. Or maybe we can have something
> like
>
> template Curry(alias tmpl, Args1...) {
> alias Curry(Args2...) = tmpl!(Args1~Args2); //Pseudocode
> }
>
> in the phobos.
It's hard to help without a minimal working example (maybe
something with just the body).
If you mean that "alias new_parser = Comb!(a, b);" generates an
error maybe that's because the correct syntax is "alias
new_parser(Range) = Comb!(a, b);" ?
One issue you may encounter is that template parameter deduction
(that's called IFTI I think) can fail with an alias (so partially
specialized template/templatized function, etc).
More information about the Digitalmars-d
mailing list