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

Regan Heath regan at netmail.co.nz
Thu Jul 4 02:25:30 PDT 2013


On Wed, 03 Jul 2013 19:10:40 +0100, bearophile <bearophileHUGS at lycos.com>  
wrote:
> Telling apart the literal for an empty array from the literal of a empty  
> but not null array is a bad idea that muds the language. And thankfully  
> this currently fails:
>
> void main() {
>      int[] emptyArray = [];
>      assert(emptyArray !is null);
> }

As this comes up often you're probably aware that there are people (like  
myself) who find the distinction between a null (non-existant) array and  
an empty array useful.

Granted, it can complicate code but only if you want the distinction, in  
all other cases you should be checking array.length == 0 which is true for  
empty and null arrays.

Also, there are ways to get around the issue which all involve having a  
separate record of whether something exists or not - perhaps by placing  
items in a associative array and using 'contains' or having a separate  
existence boolean but they're all band aids over the issue of not having  
an actual reference type.

The humble char* pointer can represent null (non-existent) , "" empty, and  
"value" and I would find it useful if string could do the same,  
consistently (there are edge cases where arrays change from empty to null  
and vice-versa).

Tommi's suggestion that all empty arrays share a common pointer is a good  
one and more or less solves the "it has to allocate memory" complaint.

So, that just leaves arguments against complexity or arguments against the  
whole concept of null/empty being useful.

R

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


More information about the Digitalmars-d mailing list