Why is array truth tied to .ptr?
Daniel Keep
daniel.keep.lists at gmail.com
Sun Dec 9 20:21:23 PST 2007
Derek Parnell wrote:
> ...
>
> 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 actually went and created a pseudo-property for just this purpose:
/// Tests the array to see if it's of non-zero length.
bool nz(T)(T arr) { return arr.length != 0; }
/// Tests the array to see if it's of zero length.
bool z(T)(T arr) { return arr.length == 0; }
if( "foo".nz() || "bar".z() ) ...
Much quicker to type, and more legible (at least, for me.)
-- Daniel
More information about the Digitalmars-d
mailing list