Issue 9792 - length field of a const SortedRange
Summary: length field of a const SortedRange
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on: 7521
Blocks:
  Show dependency treegraph
 
Reported: 2013-03-23 07:50 UTC by bearophile_hugs
Modified: 2024-12-01 16:17 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-03-23 07:50:35 UTC
I'd like this code to work:


import std.algorithm: sort;
void main() {
    const data = [1, 5, 2].sort();
    auto len = data.length;
}


DMD 2.063alpha gives:

temp.d(4): Error: mutable method std.range.SortedRange!(int[], "a < b").SortedRange.length is not callable using a const object
Comment 1 Justin Whear 2014-02-03 16:25:48 UTC
(In reply to comment #0)
> I'd like this code to work:
> 
> 
> import std.algorithm: sort;
> void main() {
>     const data = [1, 5, 2].sort();
>     auto len = data.length;
> }
> 
> 
> DMD 2.063alpha gives:
> 
> temp.d(4): Error: mutable method std.range.SortedRange!(int[], "a <
> b").SortedRange.length is not callable using a const object

Additionally `contains`, and probably `lowerBound`, `equalRange`, and `upperBound`.
Comment 2 Peter Alexander 2014-02-07 13:31:03 UTC
This is easy to fix in the library, but we'd need special code to handle it correctly, which would need to be duplicated for every function.

Once Issue 7521 is implemented, it will be solved automatically (the const will be inferred, when possible).
Comment 3 Ali Cehreli 2020-02-26 23:00:06 UTC
I ran into this in a post condition code:

import std.algorithm;

auto foo()
out (result; result.length == 1) { // <-- ERROR
  return [ 0 ].sort;
}

void main() {
}

Error: mutable method `std.range.SortedRange!(int[], "a < b").SortedRange.length` is not callable using a `const` object
Consider adding `const` or `inout` to std.range.SortedRange!(int[], "a < b").SortedRange.length

Ali
Comment 4 Witold Baryluk 2020-04-30 13:46:20 UTC
I got similar issues with constness, in other cirumstances. `opIndex` and `opSlice` could be made const too, if they return const range or SortedRange, and underlying Range is const.

I have a method like this:

void search(Range, T)(in T[] needles, in SortedRange!(Range) haystack, long[] ret, const long index_offset) {

it is called as search(needles, assumeSorted(kaystack), ret, 0);


but the body of this function does pose issues:

search.d:67:7: error: mutable method std.range.SortedRange!(const(int)[], "a < b").SortedRange.length is not callable using a const object
   67 |   if (haystack.length == 0) {
      |       ^
search.d:83:17: error: mutable method std.range.SortedRange!(const(int)[], "a < b").SortedRange.opIndex is not callable using a const object
   83 |     if (haystack[i] == needles[j]) {


This is with gdc 9.3.0-11 and libgphobos76 9.3.0-11.



It looks like automatic inference of const methods, or specialization of const and non-const (at the cost of repeating the same code twice) is required to make it work.
Comment 5 Witold Baryluk 2020-04-30 14:27:00 UTC
Same issue with `SortedRange.trisect` too.
Comment 6 dlangBugzillaToGithub 2024-12-01 16:17:05 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9964

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB