Template currying

Ed via Digitalmars-d digitalmars-d at puremagic.com
Thu May 5 16:38:38 PDT 2016


On Thursday, 5 May 2016 at 23:19:59 UTC, Yuxuan Shui wrote:
> On Thursday, 5 May 2016 at 23:12:40 UTC, Ed wrote:
>> On Thursday, 5 May 2016 at 22:53:01 UTC, Yuxuan Shui wrote:
>>> On Thursday, 5 May 2016 at 21:54:29 UTC, Ed wrote:
>>>> On Thursday, 5 May 2016 at 20:17:08 UTC, Yuxuan Shui wrote:
>>>>> [...]
>>>>
>>>> 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).
>>>
>>> Simple example:
>>>
>>>     int a(int b, T)(T c){return 0;}
>>>
>>> It's fine to:
>>>
>>>     a!1(2);
>>>
>>> But:
>>>
>>>     alias A = a!1;
>>>
>>> Would fail.
>>
>> alias aa(T) = a!(1,T);
>> aa!ubyte(2);
>>
>> Your alias declaration is not correct.
>
> As you can see in my first post, I know how to make this work. 
> I just think it'd be nice if compiler can do this automatically.
>
> Anothor point is that, what if I want to use partially 
> instantiated templates as template arguments?

My bad, I didn't read carefully "or not use alias:".
With alias template parameter you can pass template:

------
import std.stdio;

template Foo(T,U){ T pop(){return T.max;}}

alias Partial(U) = Foo!(U, string);

auto bar(alias Whatever)()
{
     alias instance = Whatever!int;
     return instance.pop;
}

void main(string[] args)
{
     writeln(bar!(Partial)());
}
------

for example.


More information about the Digitalmars-d mailing list