digitalmars.D.learn - Has truth of arrays always acted like this?
- Bill Baxter (58/58) Oct 14 2007 I could have sworn there was a big discussion just recently about how
- Bill Baxter (9/14) Oct 14 2007 I think maybe the discussion I was remembering was about comparison with...
- Mike Parker (7/25) Oct 14 2007 I recall a recent discussion specifically about empty vs. null strings,
- Kris (6/31) Oct 14 2007 Well, I sure hope that /never/ happens. A null is a null (the array .ptr...
- Bill Baxter (6/40) Oct 14 2007 Hope what never happens?
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");
}
}
Oct 14 2007
Bill Baxter wrote: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?I think maybe the discussion I was remembering was about comparison with null. Indeed (a==null) is true for both uninitialized *and* length==0 arrays. That seems completely backwards to me. If anything, a==null should be the one that checks if a has a buffer allocated at all, and if(a) should be the one that checks for any form of emptiness. Barring that, they should just behave the same. --bb
Oct 14 2007
Bill Baxter wrote:Bill Baxter wrote:I recall a recent discussion specifically about empty vs. null strings, i.e. an empty string ("") is equivalent to a null string: char[] s1 = ""; char[] s2 = null; if(s1 == s2) writefln("equal"); This will evaluate to true.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?I think maybe the discussion I was remembering was about comparison with null. Indeed (a==null) is true for both uninitialized *and* length==0 arrays. That seems completely backwards to me. If anything, a==null should be the one that checks if a has a buffer allocated at all, and if(a) should be the one that checks for any form of emptiness. Barring that, they should just behave the same. --bb
Oct 14 2007
Well, I sure hope that /never/ happens. A null is a null (the array .ptr and .length are both zero), while an empty string is perfectly valid. tip: faster way (on 32bit platforms) to check for null array is to test the .ptr only, since it's 32 bits rather than 64. "Mike Parker" <aldacron71 yahoo.com> wrote in message news:feuj8s$1kjt$1 digitalmars.com...Bill Baxter wrote:Bill Baxter wrote:I recall a recent discussion specifically about empty vs. null strings, i.e. an empty string ("") is equivalent to a null string: char[] s1 = ""; char[] s2 = null; if(s1 == s2) writefln("equal"); This will evaluate to true.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?I think maybe the discussion I was remembering was about comparison with null. Indeed (a==null) is true for both uninitialized *and* length==0 arrays. That seems completely backwards to me. If anything, a==null should be the one that checks if a has a buffer allocated at all, and if(a) should be the one that checks for any form of emptiness. Barring that, they should just behave the same. --bb
Oct 14 2007
"Mike Parker" <aldacron71 yahoo.com> wrote in message news:feuj8s$1kjt$1 digitalmars.com...Kris wrote:Bill Baxter wrote:Bill Baxter wrote:I recall a recent discussion specifically about empty vs. null strings, i.e. an empty string ("") is equivalent to a null string: char[] s1 = ""; char[] s2 = null; if(s1 == s2) writefln("equal"); This will evaluate to true.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?I think maybe the discussion I was remembering was about comparison with null. Indeed (a==null) is true for both uninitialized *and* length==0 arrays. That seems completely backwards to me. If anything, a==null should be the one that checks if a has a buffer allocated at all, and if(a) should be the one that checks for any form of emptiness. Barring that, they should just behave the same. --bbWell, I sure hope that /never/ happens. A null is a null (the array.ptr and.length are both zero), while an empty string is perfectly valid.Hope what never happens?tip: faster way (on 32bit platforms) to check for null array is totest the.ptr only, since it's 32 bits rather than 64.size_t is 32 bits on a 32 bit platform too. --bb
Oct 14 2007








Bill Baxter <dnewsgroup billbaxter.com>