Sending a templated function as an argument to a templated function in D

I am trying to send a function D sortas an argument to a function template pipe. When I use a sorttemplate without arguments, it works:

import std.stdio,std.algorithm,std.functional;

void main()
{
    auto arr=pipe!(sort)([1,3,2]);
    writeln(arr);
}

However, when I try to use sortwith the template argument:

import std.stdio,std.algorithm,std.functional;

void main()
{
    auto arr=pipe!(sort!"b<a")([1,3,2]);
    writeln(arr);
}

I get an error - main.d(5): Error: template instance sort!("b<a") sort!("b<a") does not match template declaration sort(alias less = "a < b",SwapStrategy ss = SwapStrategy.unstable,Range)

Why is this happening? sort!"b<a"runs on it, and it has the same arguments and return types as sortit does, so why pipeaccept sortbut not sort!"b<a"? And is there any correct syntax for what I'm trying to do?

UPDATE

OK, I tried to wrap a function sort. The following code works:

import std.stdio,std.algorithm,std.functional,std.array;

template mysort(string comparer)
{
    auto mysort(T)(T source)
    {
        sort!comparer(source);
        return source;
    }
}

void main()
{
    auto arr=pipe!(mysort!"b<a")([1,3,2]);
    writeln(arr);
}

? - sort ?

+5
1

, , Range.

size_t sort2(alias f, Range)(Range range)
{
    return 0;
}
alias sort2!"b<a" u;

sort!"b<a" , . sort2!"b<a"([1,2,3]) , [1,2,3] , Range int[]. " " (IFTI) ". IFTI , . sort!"b<a" , , .

, , mysort:

 auto arr = pipe!(x => sort!"b<a"(x))([1,3,2]);

. , .

auto arr = pipe!(sort!("b<a", SwapStrategy.unstable, int[]))([1,3,2]);
+5

All Articles