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)[])
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."]
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.
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