empty string & array truth values& comparisons to null

spir denis.spir at gmail.com
Sun Dec 12 02:02:34 PST 2010


Hello,


After the thread on "String to boolean inconsistency", here is a little comparison. I think it speaks by itself.

unittest {
    auto form = "%s %s   %s %s   %s %s";
    
    int[] a0;
    int[] a1 = [];
    writefln(form,
            a0?true:false, a1?true:false,
            a0 == null, a1 == null,
            a0 is null, a1 is null,
    );
    
    string s0;
    string s1 = "";
    writefln(form,
            s0?true:false, s1?true:false,
            s0 == null, s1 == null,
            s0 is null, s1 is null,
    );
    
    s1 = "".idup;
    writefln(form,
            s0?true:false, s1?true:false,
            s0 == null, s1 == null,
            s0 is null, s1 is null,
    );
    
    char[] s2 = "".dup;
    writefln(form,
            s0?true:false, s2?true:false,
            s0 == null, s2 == null,
            s0 is null, s2 is null,
    );
}

==>

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 uninitialised ones? Should .init of a collection be an empty one (or at least behave identically)?


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list