Bug 21216 – SortedRange.empty should be const, .front should be inout

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-09-02T20:51:44Z
Last change time
2024-12-01T16:37:33Z
Assigned to
No Owner
Creator
Simon Naarmann
Moved to GitHub: phobos#10437 →

Comments

Comment #0 by s.naarmann — 2020-09-02T20:51:44Z
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
Comment #1 by s.naarmann — 2020-09-08T15:34:37Z
Discussion on the D Forums: https://forum.dlang.org/thread/[email protected] Gist: Instead of adding const/inout, which might not fit every wrapped range, we should use `template this` to let the compiler figure out the appropriate annotations for empty/front/... individually for each wrapped range.
Comment #2 by robert.schadek — 2024-12-01T16:37:33Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10437 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB