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

Regan Heath regan at netmail.co.nz
Mon Dec 2 02:12:52 PST 2013


On Sat, 30 Nov 2013 04:37:00 -0000, Peter Alexander  
<peter.alexander.au at gmail.com> wrote:

> On Friday, 29 November 2013 at 15:55:57 UTC, Regan Heath wrote:
>> On Fri, 29 Nov 2013 09:51:05 -0000, Peter Alexander  
>> <peter.alexander.au at gmail.com> wrote:
>>
>>> 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;
>>
>> Resorting to pointers = fail (for the language) I'm afraid.
>>
>> R
>
> How so? Only pointer arithmetic is unsafe. Pointers themselves are  
> perfectly fine and safe.

The code using pointers is unnecessarily ugly *IMO*.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list