Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create a SortedRange from inout array #18837

Open
dlangBugzillaToGithub opened this issue Jun 11, 2014 · 2 comments
Open

Cannot create a SortedRange from inout array #18837

dlangBugzillaToGithub opened this issue Jun 11, 2014 · 2 comments

Comments

@dlangBugzillaToGithub
Copy link

David Watkins reported this on 2014-06-11T21:51:39Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=12893

CC List

Description

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)[])
@dlangBugzillaToGithub
Copy link
Author

briancschott commented on 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."]

@dlangBugzillaToGithub
Copy link
Author

post (@kyllingstad) commented on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant