Just a friendly reminder about using arrays in boolean conditions

IchorDev zxinsworld at gmail.com
Sat Nov 23 12:13:22 UTC 2024


On Monday, 18 November 2024 at 15:39:38 UTC, Dom DiSc wrote:
> One should always first check for null and then for length. 
> This should be immediately clear, as asking for a length 
> doesn't make sense if something is null.
> Ok, an array has both a pointer and a length, but I would never 
> expect the length to contain something useful, if the pointer 
> is not assigned a legal address.
> If I were to implement arrays, I would use a simple pointer, 
> and length would be the first element of the allocated block. 
> For any object, I have always in mind that it could be 
> implemented in this way, so would never access anything as long 
> as the pointer is not checked first.

Having length be 0 if the pointer is null is useful. `if(arr)` 
should be the same as `if(arr.length) IMO.


More information about the Digitalmars-d mailing list