Forbid dynamic arrays in boolean evaluation contexts

Jonathan M Davis jmdavisProg at gmx.com
Mon Mar 25 10:15:59 PDT 2013


On Monday, March 25, 2013 17:43:24 Phil Lavoie wrote:
> I do believe that, in any case, this form is best:
> if( arr !is null && !arr.empty )

That's utterly pointless. empty checks that length == 0, and length == 0 if 
the array is null. It can be useful to check whether an array is null with the 
is operator for cases where that's used to indicate that a result wasn't found 
or something like that, but very little cares about the difference between a 
null array and an empty one, and attempting to treat them as different tends to 
be very error-prone.

I don't really like how null works with arrays, since it's generally treated 
as the same as an empty array (including for ==), but that's the way it works 
in D, and you just have to deal with it. And given how arrays in D work in 
general, having if(arr) check specifically for null rather than empty is 
definitely error-prone.

- Jonathan M Davis


More information about the Digitalmars-d mailing list