-
-
Notifications
You must be signed in to change notification settings - Fork 741
Issue 11403 - functions in std.algo can't be used as pred #1676
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
Conversation
I wish this was a language feature. I think I've filed this as an enhancement somewhere. |
https://d.puremagic.com/issues/show_bug.cgi?id=9365
Hum...
I think partial instantiation is specifically not supported no? It comes with its own problems, like ambiguity ( is I should be pasting this in the issue instead of here. In any case, I think my fix is rational and stand alone. |
Right right, maybe I'd like some special syntax for it, anyway I was thinking out loud. The pull is a welcome change. |
Turns out that an overzealous constraint prevented assert(canFind([0, 1, 2, 3], 1, 3) == 1); So I fixed that too while I was at it. |
I just learned that it turns out that eponymous templates can be without specifying parameters (unlike normal templates, where you need to template canFind(alias pred = "a == b")
{
auto canFind(R, E)(R r, E e);
}
//Both of these work
canFind([1, 2, 3], 2);//Pred implicitly evaluated as "a == b"
canFind!"a > b"([1, 2, 3], 2);//Pred explicitly given So that's cool. @AndrejMitrovic : Thoughts/ping? |
LGTM. I would have wanted to check out the ddoc output but alas... |
Works like normal function templates, right? LGTM, will check the docs tomorrow. |
{ | ||
return !find!pred(range).empty; | ||
/// ditto |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. So I just mark it as ///
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work with ddox, might be a bug though. Maybe you can split the documentation.
Maybe something like "Returns true is a value ... can be found." and "Implementation for input ranges."?
Where, it would be fairly easy to simply allow more parens. bool any(alias pred)(Range)(Range range) if (is(typeof(find!pred(range))))
{
return return !find!pred(range).empty;
} template any(alias pred)
{
template any(Range) if (is(typeof(find!pred(range))))
{
bool any(Range range)
{
return return !find!pred(range).empty;
}
}
} |
I meant as opposed to non-eponymous: template Foo()
{
void bar();
}
void main()
{
Foo.bar();
} |
{ | ||
return !find!pred(haystack, needle).empty; | ||
} | ||
//Explictly Undocumented. Do not use. It may be deprecated in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is undocumented, why did you use /++
above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andralex : The first /++
documents the canFind(alias pred="a == b")
familly of functions, which contains 3 overloads:
canFind(alias pred="a == b").canFind(Range)(Range haystack); //NOT documented
canFind(alias pred="a == b").canFind(Range, Element)(Range haystack, Element needle); //documented
size_t canFind(Range, Elements...)(Range haystack, Elements needles); //documented
I need to double check how it works though, according to @MartinNowak , the generated DDoc isn't correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this works as expected, the template is documented as well as two of the three overloads.
OK: I've added the assert (also noticed the unittest was actually wrong...), and reworked the documentation of all/any. Should be better now. |
Issue 11403 - functions in std.algo can't be used as pred
Cool, thanks. Now, I gotta apply the same reasoning to a couple other functions. |
Partial fix for 11403:
http://d.puremagic.com/issues/show_bug.cgi?id=11403
This pull allows declaring
any
,all
andcanFind
with their predicates, but without args. This allows either aliasing them to a new name, or using them as a pred to yet a third function. For example:or