D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12216 - Overloading templates using alias
Summary: Overloading templates using alias
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-20 21:32 UTC by Jesse Phillips
Modified: 2024-12-13 18:17 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jesse Phillips 2014-02-20 21:32:51 UTC
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 Seb 2018-02-10 22:21:22 UTC
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 dlangBugzillaToGithub 2024-12-13 18:17:15 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18778

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB