Bug 20751 – SortedRange with ref predicate parameters fails

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-04-19T21:05:25Z
Last change time
2020-04-26T01:26:12Z
Keywords
pull
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2020-04-19T21:05:25Z
Consider a predicate that accepts ref parameters for sort: ---- import std; bool pred(ref int a, ref int b) { return a < b; } void main() { auto sortedArr = [5,4,3,2,1].sort!pred; auto rng = sortedArr.equalRange(3); } --- onlineapp.d(11): Error: template std.range.SortedRange!(int[], pred, cast(SortedRangeOptions)0).SortedRange.equalRange cannot deduce function from argument types !()(int), candidates are: /dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(10991): equalRange(V)(V value) with V = int must satisfy the following constraint: isTwoWayCompatible!(predFun, ElementType!Range, V) --- The gist of it is, isTwoWayCompatible assumes the predicate must work for rvalues. But there is no such requirement for sorting. This is quite important when sorting large structures. I don't know if this is a bug for SortedRange, or on isTwoWayCompatible. Perhaps the refness needs to be told to isTwoWayCompatible?
Comment #1 by dlang-bot — 2020-04-25T22:45:38Z
@MoonlightSentinel created dlang/phobos pull request #7457 "Fix Issue 20751 - SortedRange with ref predicate parameters fails" fixing this issue: - Fix Issue 20751 - SortedRange with ref predicate parameters fails isTwoWayCompatible should accept functions with ref parameters. https://github.com/dlang/phobos/pull/7457
Comment #2 by dlang-bot — 2020-04-26T01:26:12Z
dlang/phobos pull request #7457 "Fix Issue 20751 - SortedRange with ref predicate parameters fails" was merged into master: - 54dbc0668cdbde18103c5f720b2ba18553c8da19 by MoonlightSentinel: Fix Issue 20751 - SortedRange with ref predicate parameters fails isTwoWayCompatible should accept functions with ref parameters. https://github.com/dlang/phobos/pull/7457