D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6490 - countUntil will take a non-range for its second argument
Summary: countUntil will take a non-range for its second argument
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-14 03:33 UTC by Jonathan M Davis
Modified: 2015-06-09 05:15 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 Jonathan M Davis 2011-08-14 03:33:15 UTC
This code compiles:

import std.algorithm;

void main()
{
    string str = "abc123";

    auto i = countUntil!((a, b){return (a >= '0' && a <= '9') || a == '.';})
                         (str, 0);
}

It shouldn't. Notice that the second argument to countUntil is 0, which is an int, not a range.
Comment 1 David Nadlinger 2011-08-14 03:43:43 UTC
Are you sure that this is a bug? The documentation says that it counts until haystack.startsWith!pred(needle) is true, and startsWith is also defined for single element needles. You are right, the usefulness of this behavior is questionable, though.
Comment 2 Jonathan M Davis 2011-08-14 03:46:35 UTC
Okay. I misunderstood countUntil. Its signature is

sizediff_t countUntil(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle)
if (is(typeof(startsWith!pred(haystack, needle))))

Of course, I didn't see the template constraint in the documentation, and given
the type names, I assumed that needle had to be a range. My mistake. There's no
problem with this code.
Comment 3 Jonathan M Davis 2011-08-14 03:47:53 UTC
The real issue here IMHO is the fact that 0 implicitly converts to a dchar, but that's not going to change anytime soon. The original code that I copied this from was doing that though. I wouldn't normally put a 0 there.

In any case, this isn't a bug.