Comment #0 by Jesse.K.Phillips+D — 2014-02-20T21:32:51Z
When importing a module D allows a modules functions to be brought into the overload set by declaring an alias. Example
-------
import std.ascii;
bool isLower(bool c) {
return true;
}
alias isLower = std.ascii.isLower;
-------
Templates should allow for the same behavior:
-------
import std.range;
import std.algorithm;
import std.traits;
bool isSorted(alias less = "a < b", Range)(Range r) if (isStaticArray!Arr) {
return isSorted(r[]);
}
alias isSorted = std.algorithm.isSorted;
-------
Comment #1 by greensunny12 — 2018-02-10T22:21:22Z
This is going to be tough because some templates are written like this:
foo()
if (a)
foo()
if (!a)
There overloading is rather hard.
However, the following trick should always work:
---
bool isSorted(alias less = "a < b", Range)(Range r)
{
import std.algorithm : isSortedImpl = isSorted;
static if (isStaticArray!Range)
{
return isSortedImpl!less(r[]);
}
else
{
return isSortedImpl!less(r[]);
}
}
---
https://run.dlang.io/is/EL3VEj
Though nowadays this isn't even necessary for isSorted.
Comment #2 by robert.schadek — 2024-12-13T18:17:15Z