[:] as empty associative array literal, plus warning for null

monarch_dodra monarchdodra at gmail.com
Fri Jul 5 01:26:45 PDT 2013


On Friday, 5 July 2013 at 08:15:35 UTC, Nicolas Sicard wrote:
> On Thursday, 4 July 2013 at 23:52:35 UTC, bearophile wrote:
>> Andrei Alexandrescu:
>>
>>> Where does the whole "stronger typing" comes in? This is 
>>> poppycock. We need real arguments here.
>>
>> Maybe it's a matter of definitions, for me having "null" as 
>> literal for empty array, null pointer, empty associative 
>> array, and more is more weakly typed compared to having a 
>> literal like [] usable only for empty dynamic arrays (and 
>> strings), a literal as [:] usable only for empty associative 
>> arrays, and null for pointers, class references (and little 
>> else like a Nullable).
>>
>> Bye,
>> bearophile
>
> While I agree with the need to have a literal for 
> non-initialized arrays and another one for initialized but 
> empty arrays, that is null and [] respectively, I can't see the 
> necessity for [:]. The literal should be used to mark the 
> difference between null and empty, not the difference between 
> plain or associative, shouldn't it?
> For me, having to type
>   int[string] foo = [:];
> instead of
>   int[string] foo = []; // same semantic
> would just be a source of confusion.

Keep in mind that arrays are value that *look* like reference 
types. You can get an array that's non-null with no allocations.

Associative arrays, on the other hand, are true reference types. 
This means initialization *necessarily* implies allocating a 
payload. Having "[]" "maybe"/"maybe not" allocate is not that 
good of an idea. Using "[:]", on the other hand, really stresses 
the fact that "this AA needs to be empty but initialized, and I 
know I'm going to allocate for it".


More information about the Digitalmars-d mailing list