Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 02, 2013 Associative array references | ||||
---|---|---|---|---|
| ||||
Bug or feature? This one caught me by surprise: void main() { { int[string] aa[string]; aa["foo"]["bar"] = 1; assert(aa["foo"]["bar"] == 1); //ok auto aa2 = aa; aa2["boo"]["dar"] = 2; assert(aa["foo"]["bar"] == 1); //ok } { int[string] aa[string]; //not set to anything yet auto aa2 = aa; aa2["boo"]["dar"] = 2; assert(aa["foo"]["bar"] == 1); //oops } } It seems that assigning an AA to another makes both point at the same data only if the first array has data to begin with. Is that the expected behaviour? Atila |
December 02, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote:
> Bug or feature? This one caught me by surprise:
>
> void main() {
> {
> int[string] aa[string];
> aa["foo"]["bar"] = 1;
> assert(aa["foo"]["bar"] == 1); //ok
>
> auto aa2 = aa;
> aa2["boo"]["dar"] = 2;
> assert(aa["foo"]["bar"] == 1); //ok
> }
>
> {
> int[string] aa[string]; //not set to anything yet
>
> auto aa2 = aa;
> aa2["boo"]["dar"] = 2;
> assert(aa["foo"]["bar"] == 1); //oops
> }
> }
>
> It seems that assigning an AA to another makes both point at the same data only if the first array has data to begin with. Is that the expected behaviour?
>
> Atila
I would say it is consequence of current implementation, and, taking into account ongoing efforts to rewrite AA implementation, this special detail of null AA behavior may one day be gone.
|
December 02, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | Atila Neves:
> int[string] aa[string];
Don't mix C style array definitions with D style ones :-) Always use the D-style, unless you are porting C code (and refactor it later).
Bye,
bearophile
|
December 02, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 2 December 2013 at 15:13:48 UTC, bearophile wrote:
> Atila Neves:
>
>> int[string] aa[string];
>
> Don't mix C style array definitions with D style ones :-) Always use the D-style, unless you are porting C code (and refactor it later).
How would that go in this case?
|
December 02, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | Atila Neves:
> How would that go in this case?
An example usage:
void main() {
int[float][string] aa;
aa["foo"][1.5] = 1;
}
Bye,
bearophile
|
December 03, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote: > It seems that assigning an AA to another makes both point at the same data only if the first array has data to begin with. Is that the expected behaviour? > > Atila You've been hit by the null associative array not being the same as an empty associative array. See discussion: http://forum.dlang.org/post/wovnuggqtscsbgnjcpua@forum.dlang.org |
December 03, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Tuesday, 3 December 2013 at 03:14:46 UTC, Jesse Phillips wrote:
> On Monday, 2 December 2013 at 13:30:44 UTC, Atila Neves wrote:
>> It seems that assigning an AA to another makes both point at the same data only if the first array has data to begin with. Is that the expected behaviour?
>>
>> Atila
>
> You've been hit by the null associative array not being the same as an empty associative array. See discussion:
>
> http://forum.dlang.org/post/wovnuggqtscsbgnjcpua@forum.dlang.org
Sigh. If at least there was a way to declare them empty...
|
December 03, 2013 Re: Associative array references | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 2 December 2013 at 16:52:51 UTC, bearophile wrote:
> Atila Neves:
>
>> How would that go in this case?
>
> An example usage:
>
> void main() {
> int[float][string] aa;
> aa["foo"][1.5] = 1;
> }
>
> Bye,
> bearophile
Oh. That makes sense. Doh!
I guess I was focussing on "key to AA of..." and that's why I wrote it that way.
|
Copyright © 1999-2021 by the D Language Foundation