empty arrays and cast(bool): WAT

Daniel Murphy yebblies at nospamgmail.com
Sat Feb 18 22:06:19 PST 2012


I agree, and I doubt it was intentional.

if (arr) should mean if (arr.length && arr.ptr)
But since you can only get (arr.length != 0 && arr.ptr == null) when doing 
unsafe things with arrays, I think it's entirely reasonable to use
if (arr) -> if (arr.length)

This is what I expected.

Much like with class references, distinguishing between null and empty is 
not '=='s job, that is what 'is' is for.

"Timon Gehr" <timon.gehr at gmx.ch> wrote in message 
news:jhpl6j$2m6c$1 at digitalmars.com...
> Why does the following code behave funny?
>
> void main(){
>     string x = " "[1..1];
>     writeln(cast(bool)x); // true
>     char[] y = [' '][1..1];
>     writeln(cast(bool)y); // false
> }
>
> Is there any reason for empty arrays to evaluate to true? This is very bug 
> prone. 




More information about the Digitalmars-d mailing list