Bug 12893 – Cannot create a SortedRange from inout array

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-06-11T21:51:39Z
Last change time
2024-12-13T18:21:27Z
Keywords
spec
Assigned to
No Owner
Creator
David Watkins
Moved to GitHub: dmd#18837 →

Comments

Comment #0 by David.L.Watkins — 2014-06-11T21:51:39Z
import std.algorithm; import std.array; import std.range; struct Data { string name; alias name this; } inout(Data) findData(inout(Data)[] arr, string name) { auto sorted = assumeSorted(arr); auto found = sorted.equalRange(name); return found.empty ? Data.init : found.front; } void main() { Data[] arr; arr ~= Data("Alice"); arr ~= Data("Carol"); arr ~= Data("Bob"); sort(arr); auto data = findData("Bob"); } Fails with: \src\phobos\std\range.d(8340): Error: variable std.range.SortedRange!(inout(Data)[], "a < b").SortedRange._input only parameters or stack based variables can be inout \src\phobos\std\range.d(8839): Error: template instance std.range.SortedRange!(inout(Data)[], "a < b") error instantiating instantiated from here: assumeSorted!("a < b", inout(Data)[])
Comment #1 by briancschott — 2014-08-01T21:25:44Z
Related: import std.algorithm; import std.stdio; struct S { void x() inout { writeln(y.map!(a => a + 1)); } int[] y; } void main() { const S s; s.x(); } /usr/include/dmd/phobos/std/algorithm.d(474): Error: variable test.S.x.MapResult!(__lambda1, inout(int)[]).MapResult._input only parameters or stack based variables can be inout /usr/include/dmd/phobos/std/algorithm.d(427): Error: template instance test.S.x.MapResult!(__lambda1, inout(int)[]) error instantiating test.d(6): instantiated from here: map!(inout(int)[]) Failed: ["dmd", "-v", "-o-", "test.d", "-I."]
Comment #2 by post — 2016-02-10T21:20:57Z
I think these are problems with the design of 'inout' rather than with Phobos. Perhaps it could be worked around with careful use of std.typecons.Unqual, but that seems unfeasible to do, as there are *a lot* of types that have this problem. (Almost all ranges, for example, at least the ones that have internal caching.) Therefore, I'm recategorising this as a DMD/spec issue. Here's a test case that doesn't involve Phobos: struct Ptr(T) { T* ptr; alias ptr this; } Ptr!T takePtr(T)(ref T obj) { return Ptr!T(&obj); } inout(int[]) fun(inout int[] arr) { auto p = takePtr(arr); return *p; } I wonder if we could invent a rule by which inout gets converted to const in some places.
Comment #3 by robert.schadek — 2024-12-13T18:21:27Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18837 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB