if(arr) now a warning

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 10 05:53:43 PDT 2015


On Thu, 09 Apr 2015 16:59:00 -0400, Steven Schveighoffer wrote:

> A confusing and dangerous "feature" of D was to treat the expression
> if(arr) as meaning if(arr.ptr).
> 
> Anyone coming from any number of other languages may think that if(arr)
> means "if arr has any elements". Typically one cares what is inside an
> array, not where it lives.
> 
> However, while checking to see if an array has elements is very similar
> to checking for a null pointer (obviously, a null pointer array should
> have no elements), it's not equivalent. Therefore, when attempting to
> fix this confusion, we had to disable the if(arr) check altogether. You
> must now specify what part of the array you care about, if(arr.ptr) or
> if(arr.length).
> 
> As usual with changes of this nature, there are differing opinions, and
> differing styles. I personally never use null as a special value for an
> array, but others do. They use if(arr) to actually mean if(arr.ptr). For
> these cases, the update will cause some amount of busywork changing
> these checks.
> 
> But when updating Phobos to comply with this change, we found several
> actual incorrect usages. So I think the change is worth it, and not much
> busywork. YMMV.
> 
> What do you think?

this is good, as `if (arr.ptr)` is really confusing. yet i can't 
understand why don't rewrite it to `if (arr.length)` instead.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150410/d63a17d6/attachment.sig>


More information about the Digitalmars-d mailing list