Why is array truth tied to .ptr?
Bill Baxter
dnewsgroup at billbaxter.com
Sun Dec 9 21:33:37 PST 2007
Derek Parnell wrote:
> 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.
I'm just too lazy to type that much. :-)
--bb
More information about the Digitalmars-d
mailing list