What is the current point to empty/null associative arrays?

Peter Alexander peter.alexander.au at gmail.com
Fri Nov 29 01:51:05 PST 2013


On Friday, 29 November 2013 at 09:39:57 UTC, Cooler wrote:
> On Friday, 29 November 2013 at 08:48:03 UTC, Chris Cain wrote:
>> On Friday, 29 November 2013 at 08:32:12 UTC, Cooler wrote:
>>> ...
>>
>> Try making fill array look more like this:
>>
>> void fillArray(ref string[int] a) { a[10] = "A"; }
>>
>> The issue is that an array (and/or associative array) is a 
>> value type. Basically, you can look at it as a struct with a 
>> pointer (and some extra info). If you don't pass it as a ref 
>> then reallocations (such as what happens when you add an item 
>> to an empty AA) will cause the two to not point to the same 
>> information anymore.
>
> Adding "ref" is not an exit. I show this example only for 
> simplicity. In my real example I have to fill different AA base 
> on condition:
>   string[int] aa1, aa2;
>   ...
>   auto aaToFill = someCheck ? aa1 : aa2;
>   // Then do something with aaToFill
>
> If aa1 is empty it will never be filled.

string[int]* aaToFill = someCheck ? &aa1 : &aa2;
(*aaToFill)["A"] = 1;


More information about the Digitalmars-d mailing list