[Issue 13670] bug in assigning to dynamic array element

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Nov 1 17:46:28 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13670

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #1 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
So, essentially, you want to do something like

a[i] = i++

and have that be equivalent to

i++;
a[i] = i;

In C++,

a[i] = i++;

the order of evaluation is undefined, so it results in undefined behavior.
AFAIK, this is also the case in D. Making it so that the order of evaluation is
always left-to-right has been discussed, but AFAIK, it's not actually what the
language currently does or the spec requires. It may be that that will change
in the future, but I don't know.

Regardless, I'd advise that you avoid any code where you mutate a variable in
an expression and then use that same variable elsewhere in the expression. Even
if the order of evaluation does get defined in D at some point in the future,
it won't be in other languages, so getting in the habit of relying on it is
probably a bad idea.

In any case, AFAIK, your example does not currently count as a bug.

--


More information about the Digitalmars-d-bugs mailing list