Bug 15202 – filter and randomCover do not work together

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-10-14T12:46:00Z
Last change time
2015-10-15T07:13:01Z
Assigned to
nobody
Creator
ryan

Comments

Comment #0 by ryan — 2015-10-14T12:46:55Z
--- int val = [1,2,3] .randomCover .filter!(x => x > 99) .front; --- I would expect that to fail -- there is no element greater than 99. Instead, it randomly retrieves one of the 3 values, ignoring the filter.
Comment #1 by ag0aep6g — 2015-10-14T19:56:13Z
You're calling `front` without checking `empty`. The range documentation [1] says: "Calling r.front is allowed only if calling r.empty has, or would have, returned false." There's no mention of range implementations having to throw an exception in such a case, so `filter`'s behavior isn't wrong. I'm closing this as invalid. If you'd like `filter` to throw an exception here, please file a new issue with severity set to "enhancement". [1] http://dlang.org/phobos/std_range_primitives.html
Comment #2 by issues.dlang — 2015-10-15T07:13:01Z
In the majority of cases, ranges either assert that the range is not empty when front is called, or they don't check at all, and I wouldn't expect that to change - not only because of efficiency concerns, but also because of @nogc (and exceptions currently require the GC). It's simply considered a logic error to call front or popFront on an empty range. So, if there's any chance that a range is going to be empty when you're looking to call front or popFront, then you need to check empty first.