digitalmars.D - empty string & array truth values& comparisons to null
- spir (48/48) Dec 12 2010 Hello,
Hello, After the thread on "String to boolean inconsistency", here is a little com= parison. I think it speaks by itself. unittest { auto form =3D "%s %s %s %s %s %s"; =20 int[] a0; int[] a1 =3D []; writefln(form, a0?true:false, a1?true:false, a0 =3D=3D null, a1 =3D=3D null, a0 is null, a1 is null, ); =20 string s0; string s1 =3D ""; writefln(form, s0?true:false, s1?true:false, s0 =3D=3D null, s1 =3D=3D null, s0 is null, s1 is null, ); =20 s1 =3D "".idup; writefln(form, s0?true:false, s1?true:false, s0 =3D=3D null, s1 =3D=3D null, s0 is null, s1 is null, ); =20 char[] s2 =3D "".dup; writefln(form, s0?true:false, s2?true:false, s0 =3D=3D null, s2 =3D=3D null, s0 is null, s2 is null, ); } =3D=3D> false false true true true true false true true true true false false false true true true true false false true true true true Generally, should empty collections be considered false? What about uniniti= alised ones? Should .init of a collection be an empty one (or at least beha= ve identically)? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 12 2010