if(arr) now a warning

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 9 15:56:23 PDT 2015


On Thu, Apr 09, 2015 at 04:59:00PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> A confusing and dangerous "feature" of D was to treat the expression
> if(arr) as meaning if(arr.ptr).
[...]
> 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).
[...]
> 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.
[...]

I agree it's worth it. It's one of the very legitimate reasons for
breaking code IMO. Or rather, to bring to the user's attention code that
likely already contains latent bugs.

Sure some people will complain about it, but in the long run, I think
they will come to appreciate the improved code correctness that this
confers.

It's akin to a similar change sometime ago that made bare pointers in
if-conditions illegal: writing `if (ptr && ...)` will cause the compiler
to complain loudly. When this first got in, one of my projects broke,
and I was rather annoyed at the time. However, when I got around to
fixing it, I found that I had to write `if (ptr !is null && ...)`
instead, which greatly clarifies the intent and readability of the code.
Since then, I have come to appreciate this change.

So this gets a thumbs up from me.


T

-- 
The computer is only a tool. Unfortunately, so is the user. -- Armaphine, K5


More information about the Digitalmars-d mailing list