Why is array truth tied to .ptr?

Derek Parnell derek at nomail.afraid.org
Sun Dec 9 19:29:19 PST 2007


On Mon, 10 Dec 2007 10:52:00 +0900, Bill Baxter wrote:
 
> In a nutshell,
>     if(array)
> is synonymous with
>     if(array.ptr)
> but I believe it makes much more sense for it to be synonymous with
>     if(array.length)
> since that's the thing you want to be checking 99.9% of the time.

I'm guessing that this behaviour is due to the commonly used idiom for all
reference types, namely that the test is against the 'pointer' value rather
than any other aspect of the item being referenced.

   class Foo {};

   Foo f = new Foo();
   Foo g;
   if (f) ...
   if (g) ...

   long *lp;
   if (lp) ...

Personally, I regard this as a shorthand option that should be avoided on
legibility grounds. 

I prefer the more explicit versions ...

   if (array.ptr !is null)
   if (array.length != 0) 
   if (f !is null)
   if (lp !is null)

only because this is harder to misunderstand.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
10/12/2007 2:17:38 PM



More information about the Digitalmars-d mailing list