Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

length field of a const SortedRange #9964

Open
dlangBugzillaToGithub opened this issue Mar 23, 2013 · 5 comments
Open

length field of a const SortedRange #9964

dlangBugzillaToGithub opened this issue Mar 23, 2013 · 5 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-03-23T07:50:35Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=9792

CC List

  • acehreli
  • justin
  • n8sh.secondary
  • peter.alexander.au (@Poita)
  • witold.baryluk+d (@baryluk)

Description

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
@dlangBugzillaToGithub
Copy link
Author

justin commented on 2014-02-03T16:25:48Z

(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`.

@dlangBugzillaToGithub
Copy link
Author

peter.alexander.au (@Poita) commented on 2014-02-07T13:31:03Z

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).

@dlangBugzillaToGithub
Copy link
Author

acehreli commented on 2020-02-26T23:00:06Z

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

@dlangBugzillaToGithub
Copy link
Author

witold.baryluk+d (@baryluk) commented on 2020-04-30T13:46:20Z

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.

@dlangBugzillaToGithub
Copy link
Author

witold.baryluk+d (@baryluk) commented on 2020-04-30T14:27:00Z

Same issue with `SortedRange.trisect` too.

@LightBender LightBender removed the P3 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants