associative arrays

simendsjo simendsjo at gmail.com
Sun Jan 8 04:06:06 PST 2012


On 08.01.2012 12:57, Jonathan M Davis wrote:
> On Sunday, January 08, 2012 12:35:27 simendsjo wrote:
>> On 08.01.2012 12:18, Jonathan M Davis wrote:
>>> On Sunday, January 08, 2012 11:33:51 simendsjo wrote:
>>>> I was thinking it could be a shorthand for the following:
>>>>
>>>> auto item = key in aa;
>>>> if (item) key.remove(key);
>>>
>>> I take it that you then intend to use item after that? I'm not sure that
>>> item is actually valid at that point. It points to memory which _was_
>>> inside of aa but may or may not be now, depending on the AA
>>> implementation, and it may or may not be reused by aa. Because it's on
>>> the GC heap, the memory itself won't be invalid, but the memory could
>>> be reused by the AA for another element, depending on its
>>> implementation. If you're lucky, it's memory that's just sitting on the
>>> GC heap and now unrelated to the AA, but I wouldn't bet on it.
>>>
>>> I would _not_ consider that to be good code. It's just asking for
>>> trouble.
>>>
>>> - Jonathan M Davis
>>
>> Ah.. This should be documented in http://dlang.org/hash-map.html
>> The only documentation for remove is:
>> "
>> Particular keys in an associative array can be removed with the remove
>> function:
>>       b.remove("hello");
>> "
>
> I confess that it's one of those things that I would have thought would have
> been fairly obvious, but it certainly wouldn't hurt to add it. The
> documentation does tend to be sparse of details in any case.
>
> - Jonathan M Davis

Certainly not obvious to me :)

auto c = new C();
C[string] aa;
aa["c"] = c;
aa.remove("c");

I guess using c from this point is valid, but if the only reference to c 
was inside the aa (and we got c using `in`), using c would have been 
undefined..?



More information about the Digitalmars-d-learn mailing list