Comment #0 by john.loughran.colvin — 2015-10-21T11:22:00Z
SortedRange doesn't check that it's predicate actually works unless you use one of lowerbound, upperBound etc. or you compile with -debug, in which case things will fail internally to std.functional.binaryFun due to the usage of the predicate in dbgVerifySorted.
Note that people could be signalling sorted-ness using SortedRange, not considering they have invalid predicates, e.g.
struct S { int a,b; }
void foo(R)(R r)
if(isInstanceOf!(SortedRange, P) && is(ElementType!R == S))
{
//assumes some idea of sorted-ness
}
only(S(0,1), S(0,4), S(3,4), S(4,4)).assumeSorted.foo();
but I guess it's OK to break that because it was technically incorrect, if rather opaquely so....
Comment #1 by jack — 2015-10-26T01:52:40Z
I would consider this as "working as intended", as the checks are only supposed to happen in debug mode. In a PR of mine, I asked @schveiguy if it made any sense to have any checks in a function that is supposed to be assuming something. He said it's strange, but it's nice to have something that double checks for you, in debug mode only.
Comment #2 by john.loughran.colvin — 2015-10-26T07:36:52Z
(In reply to Jack Stouffer from comment #1)
> I would consider this as "working as intended", as the checks are only
> supposed to happen in debug mode. In a PR of mine, I asked @schveiguy if it
> made any sense to have any checks in a function that is supposed to be
> assuming something. He said it's strange, but it's nice to have something
> that double checks for you, in debug mode only.
It's fine to have extra runtime checks in debug mode that check data, but I think the API should not change. The predicate should either always be verified to be valid or be optional.
Comment #3 by dlang-bugzilla — 2017-07-21T10:30:29Z
Verifying the predicate statically regardless of -debug does sound like a good idea.
Comment #4 by dlang-bugzilla — 2017-07-21T10:30:41Z
*** Issue 15537 has been marked as a duplicate of this issue. ***
Comment #5 by dlang — 2019-09-29T11:44:11Z
An other solution might be, to add an (optional) option parameter, like in https://dlang.org/phobos/std_range.html#transposed. The parameter decides if sortedness should be striktly checked (O(n)), roughly checked (O(log(n)) or assumed.