Has truth of arrays always acted like this?
Bill Baxter
dnewsgroup at billbaxter.com
Sun Oct 14 17:41:02 PDT 2007
I could have sworn there was a big discussion just recently about how
"null" and "empty" were the same thing with D arrays and some people
were arguing that should change. But at least with the most recent D
v1.022, it seems the truth value of uninitialized and empty arrays *are*
different. Has it always been that way?
Output:
a = [] (not initialized)
if(a) -> false
if(!a) -> true
a = [1]
if(a) -> true
if(!a) -> false
a = [] (cleared)
if(a) -> true <<== surprise! (to me)
if(!a) -> false
Program:
//------------------------------------------
module arrayif;
import std.stdio;
void main()
{
int[] a;
writefln("a = %s (not initialized)", a);
if (a) {
writefln(" if(a) -> true");
} else {
writefln(" if(a) -> false");
}
if (!a) {
writefln(" if(!a) -> true");
} else {
writefln(" if(!a) -> false");
}
a ~= 1;
writefln("a = %s", a);
if (a) {
writefln(" if(a) -> true");
} else {
writefln(" if(a) -> false");
}
if (!a) {
writefln(" if(!a) -> true");
} else {
writefln(" if(!a) -> false");
}
a.length = a.length-1;
writefln("a = %s (cleared)", a);
if (a) {
writefln(" if(a) -> true");
} else {
writefln(" if(a) -> false");
}
if (!a) {
writefln(" if(!a) -> true");
} else {
writefln(" if(!a) -> false");
}
}
More information about the Digitalmars-d-learn
mailing list