Why does compose from std.functional return a templated function

Jan Hönig hrominium at gmail.com
Wed Sep 16 10:58:20 UTC 2020


On Wednesday, 16 September 2020 at 10:50:06 UTC, Daniel Kozak 
wrote:
> On Wed, Sep 16, 2020 at 12:00 PM Jan Hönig via 
> Digitalmars-d-learn < digitalmars-d-learn at puremagic.com> wrote:
>
>> ...
>>
>> My main question is why? Is there something, which I am 
>> missing, that explains, why it is beneficial to return a 
>> templated function?
>>
>> (maybe, because I might want to compose together templated
>> non-initialized functions?)
>>
>
> It has to be templated because than you can alias it and use it 
> many times something like
>
> import std.stdio;
> import std.functional : compose;
> import std.algorithm.comparison : equal;
> import std.algorithm.iteration : map;
> import std.array : split, array;
> import std.conv : to;
>
> alias StrArrToIntArr = compose!(array,map!(to!int), split);
> void main()
> {
>     auto str1 = "2 4 8 9";
>     int[] intArr = StrArrToIntArr(str1);
> }
>
>
> If compose would not be template it would need to store 
> functions addresses so it would need to have some array of 
> functions, this would be ineffective and need to use GC

Right, if i give it non-initialized templated functions, it makes 
a lot of sense to return a template function as well.
But for functions without templates? Probably not a frequent 
usecase I guess.


More information about the Digitalmars-d-learn mailing list