Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloading templates using alias #18778

Open
dlangBugzillaToGithub opened this issue Feb 20, 2014 · 1 comment
Open

Overloading templates using alias #18778

dlangBugzillaToGithub opened this issue Feb 20, 2014 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Jesse Phillips reported this on 2014-02-20T21:32:51Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12216

CC List

  • Seb

Description

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;
-------
@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant