Behaviour of AAs after initialization

via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 7 12:33:33 PDT 2014


On Thursday, 7 August 2014 at 19:27:10 UTC, Ary Borenszweig wrote:
> On 8/7/14, 3:57 PM, Andrei Alexandrescu wrote:
>> On 8/7/14, 10:35 AM, Puming wrote:
>>> On Thursday, 7 August 2014 at 16:53:24 UTC, H. S. Teoh via 
>>> Digitalmars-d
>>>> It's really just the .init value of null which causes odd 
>>>> behaviour with
>>>> empty AA's. Fun fact:
>>>>
>>>>    void changeAA(int[string] aa) {
>>>>        aa["a"] = 123;
>>>>    }
>>>>
>>>>    // Null AA:
>>>>    int[string] aa1; // null
>>>>
>>>>    assert(aa1.length == 0);
>>>>    changeAA(aa1);    // no effect
>>> for most of the new users the WAT part is actually here :-)
>>
>> One function we could and should use is one that makes an AA 
>> that is
>> empty but not null. Right now one needs to use goofy methods 
>> such as
>> adding and then removing a key. -- Andrei
>
> It still won't be intuitive for newcomers or for anyone not 
> knowing that function.
>
> I would invert it: declaring an associative array makes it 
> non-null. Then you can choose, with a function, to initialize 
> to null.
>
> This would follow the principle of least surprise.

It cannot be done, unfortunately. Think about struct members. 
Their init value needs to be known at compile time, so the best 
we could achieve would be to have all instances point to the same 
AA by default, which is worse than what we have now.


More information about the Digitalmars-d mailing list