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

Regan Heath regan at netmail.co.nz
Thu Jul 4 02:32:40 PDT 2013


On Wed, 03 Jul 2013 19:15:19 +0100, Diggory <diggsey at googlemail.com> wrote:
> I would go as far as to say it should be impossible, or at least fairly  
> difficult in safe code to distinguish (both intentionally and  
> unintentionally) between an array of size zero with a non-null pointer  
> and an array of size zero with a null pointer, and similar for  
> associative arrays too.

Being able to represent both non-existent (null) and empty ([]) is a  
useful property, one that pointers and true references have.  If D can  
give us that power with arrays /and/ do it safely, then why wouldn't we  
want that?

If you don't care about the distinction between null and empty, you just  
check array.length == 0.

If you do care about the distinction you should be able to consistently  
tell them apart, and they should not mutate from one to the other as a  
side effect of other operations (as they currently do).

Statements like if "(array is null)"/"(array !is null)" should be used to  
check for a null (non-existent) array, and empty arrays should not match.

The main argument against having a distinction in the past has been that  
to have an empty array you need a non-null array pointer, which means you  
may have to allocate something which is a bit wasteful if you don't really  
care about the difference.

Tommi made a suggestion earlier in the thread that all empty arrays could  
point to the same global/thread local 1 byte block of pre-allocated  
memory, and this neatly solves the issue/complaint I believe.

R

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


More information about the Digitalmars-d mailing list