Per http://forum.dlang.org/thread/[email protected]:
void main()
{
import std.container;
struct S
{
int i;
}
Array!S array = [ S(0) ];
array[0].i = 1;
assert(array[0].i == 1);
}
This fails because opIndex returns by value and then the lax rules of accessing members for rvalues masquerade the member as a lvalue, which can be subsequently assigned to.
Current stance of D is that ref results cannot be escaped (not enforced yet statically but it will) so it's safe to return a ref.
Comment #1 by monarchdodra — 2014-01-10T00:50:19Z
Is this really Array that is at fault here?
//----
struct S{int i;}
S s();
int i();
void main()
{
s().i = 1; //YES.
i() = 1; //NO.
}
//----
If you can't assign to an rvalue, I question the ability to assign to an rvalue member...
Comment #2 by monarchdodra — 2014-01-13T11:47:02Z
@andrei:
Un-sealing containers is probably a correct solution, but it still feels like working around the root issue.
I filed this, which I think should also be addressed.
https://d.puremagic.com/issues/show_bug.cgi?id=11920
Comment #3 by andrei — 2014-01-13T11:48:39Z
But sealing is effectively achieved if we prohibit taking the address of ref results.