Issue 2871 - Take assumes that R.opIndex(uint) returns an lvalue.
Summary: Take assumes that R.opIndex(uint) returns an lvalue.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-21 15:28 UTC by David Simcha
Modified: 2015-06-09 01:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2009-04-21 15:28:54 UTC
import std.range, std.algorithm;

void main() {
    auto r = iota(0, 10, 1);
    assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][]));
    r = iota(0, 11, 3);
    assert(equal(r, [0, 3, 6, 9][]));
    assert(r[2] == 6);
}

C:\dmd\windows\bin\..\..\src\phobos\std\range.d(1184): Error: this._input.opIndex(index) is not an lvalue
C:\dmd\windows\bin\..\..\src\phobos\std\range.d(4): Error: template instance std.range.iota!(int,int,int) error instantiating

This is caused by 

    static if (isRandomAccessRange!(R))
        ref ElementType!(R) opIndex(uint index)
        {
            enforce(_maxAvailable > index);
            return _input[index];
        }

in std.range.  What is needed is some compile time reflection to determine whether a function returns by reference or value.
Comment 1 Koroskin Denis 2009-04-21 16:03:53 UTC
Can't auto help here?

    static if (isRandomAccessRange!(R))
        auto opIndex(uint index)
        {
            enforce(_maxAvailable > index);
            return _input[index];
        }
Comment 2 David Simcha 2010-06-17 18:51:56 UTC
This one's been fixed for ages.  I don't know how it's slipped under the radar for so long.