www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20751] New: SortedRange with ref predicate parameters fails

https://issues.dlang.org/show_bug.cgi?id=20751

          Issue ID: 20751
           Summary: SortedRange with ref predicate parameters fails
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

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?

--
Apr 19