On Saturday, 9 April 2016 at 18:27:11 UTC, ag0aep6g wrote:
On Saturday, 9 April 2016 at 18:06:52 UTC, Uranuz wrote:
Thanks. It's clear now. AA holds not `array struct` itself
inside, but pointer to it.
How the array is stored in the AA doesn't matter, as far as I
can see. The point is that you obtain a pointer to the array
struct in the AA, not a copy.
If you had tried it like the following, mapElem would be a copy
of the array struct in the AA, and the append would not affect
mapka["item"]:
----
string[] mapElem = "item" in mapka ? mapka["item"] :
(mapka["item"] = []);
mapElem ~= ["dog", "cat", "horse", "penguin", "fish", "frog"];
----
So reallocation affects ptr to allocated memory but not
pointer to `array struct`. I think that's it.
Correct.
Another observation is illustrated with the foloving code:
http://dpaste.dzfl.pl/8d68fd5922b7
Because AA and arrays are not created before they were assigned
some value it leads to inconsistency in behavior. And will
produce unexpected and hidden bugs that is not good. It's not
very good side of D's array and AA. But classes could be also
affected by this *feature* (or bug, as you wish). So we must
always construct reference semantics types before passing them to
functions that will modify it. For classes it's obvious but for
AA and dynamic arrays is not.
Another solution is to pass reference types by *ref*. So you will
not have such bugs in implementation