Forbid dynamic arrays in boolean evaluation contexts
bearophile
bearophileHUGS at lycos.com
Mon Mar 25 09:14:16 PDT 2013
Phil Lavoie:
> IMHO, somebody coming from a C/C++ background (like me) has no
> problem realizing that if( var ) means either if not null or if
> not 0.
Some D programmers come from other languages, where if(arr) means
if its len is different from zero.
D dynamic arrays are 2 words, so you can have a length zero with
a not-null pointer. So there is space for some semantic confusion.
> There was talk about changing the behavior of if( arr ) to mean
> if( !arr.empty ) but I believe this is the worst thing to do,
> since it would incorporate some inconsistencies with usual
> pointers.
D dynamic arrays aren't pointers, they are composed of two words,
so you can't assume the semantics is exactly the same.
I think assuming if(arr) means if(!arr.empty) is still better
than the current situation :-)
Bye,
bearophile
More information about the Digitalmars-d
mailing list