D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12893 - Cannot create a SortedRange from inout array
Summary: Cannot create a SortedRange from inout array
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2014-06-11 21:51 UTC by David Watkins
Modified: 2024-12-13 18:21 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Watkins 2014-06-11 21:51:39 UTC
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 briancschott 2014-08-01 21:25:44 UTC
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 Lars T. Kyllingstad 2016-02-10 21:20:57 UTC
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 dlangBugzillaToGithub 2024-12-13 18:21:27 UTC
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