Compiling the code below gives these errors:

main.d(92): Error: cast(inout(int))this.list.data[cast(uint)(this.pos + i)] is not an lvalue main.d(101): Error: template instance main.Iterator!(const(List!int)) error instantiating
main.d(108): instantiated from here: first!(const(List!int))

struct Iterator(C)
{
    C list;
    int pos;
        
    alias T = typeof(list.data[0]);

    this(C list, int pos) {
        this.list = list;
        this.pos = pos;
    }

    ref inout(T) opIndex(int i) inout {
        return list.data[pos + i];
    }
}

class List(T)
{
    T[] data = new T[1];

    auto first(this This)() {
        return Iterator!This(this, 0);
    }
}

int main(string[] argv)
{
    const(List!int) c;
    auto it = c.first;
}

Is it not possible to return a ref from an inout function?


Reply via email to