www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21216] New: SortedRange.empty should be const, .front should

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

          Issue ID: 21216
           Summary: SortedRange.empty should be const, .front should be
                    inout
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: eiderdaus gmail.com

Example code that fails to compile with DMD 2.093.0 on Linux 64-bit:

    import std.range;

    class C { }

    struct A {
    private:
        SortedRange!(C[]) _backend;

    public:
        inout(C) front() inout {
            return _backend.front();
        }

        bool empty() const {
            return _backend.empty();
        }
    }

    void main() { }

This generates two errors: Can't call front() on an inout _backend, and can't
call empty() on const _backend. The compiler even suggests annotating
SortedRange's methods with const/inout.

Expected instead: The above code compiles.

Workaround: Replace "SortedRange!(C[]) _backend;" with "C[] _backend;", and
design struct A to ensure that _backend is always sorted.

In Phobos's source for std.range.SortedRange, empty() is declared non-const,
but has a comment "//const". This has been untouched since at least 2014.

---

Related bug:
https://issues.dlang.org/show_bug.cgi?id=9792
length field of a const SortedRange

--
Sep 02 2020