Truth value of an empty slice

MrOrdinaire mrordinaire at gmail.com
Wed Jan 9 04:37:47 PST 2013


Hi,

I am not sure why the following code would throw a range 
violation whenever haystack does not contain needle.

int[] find(int[] haystack, int needle)
{
     while (haystack && haystack[0] != needle)
         haystack = haystack[1 .. $];
     return haystack;
}

Testing for haystack.length > 0 instead of haystack will make the 
code work.

Does it mean that the truth value of an empty slice depends on 
the underlying array, not on the number of elements the slice 
has, i.e. its length?

Thanks,
Minh


More information about the Digitalmars-d-learn mailing list