www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Assignment index operator with a class returned from a property

I think the best way to illustrate this is with code:

class Foo
{
   this() {
     bar_ = new Bar;
   }

   Bar bar() {
     return bar_;
   }

private:
   Bar bar_;
}

class Bar
{
   int opIndex(int key) { return 0; /* Do something here */ }
   int opIndex(int key, int value) { return 0; /* Do something here */ }
}

Assuming Foo f = new Foo;
Using the first operator overload like so:

int i = f.bar[0];

This works fine. While using the second does not:

f.bar[0] = 100;

And the compiler generates: " 'f.bar().opIndex(20)' is not an lvalue "
Calling the property explicitly works though:

f.bar()[0] = 100;
Apr 27 2004