What about putting array.empty in object.d?
Jonathan M Davis
jmdavisProg at gmx.com
Wed Mar 21 11:33:58 PDT 2012
On Wednesday, March 21, 2012 15:54:51 Daniel Murphy wrote:
> FWIW, I would rather see `if (array)` translated to `if (array.length)` and
> this become the recomended way to check if an array is empty. Wouldn't that
> remove the dependency on std.array for most of the cases?
The problem with checking whether length == 0 is that it's inefficient for some
containers, so it's generally good practice to use empty rather than length.
And while length == 0 is fine for arrays, it promotes bad habits in general, so
I'm against it and think that code should pretty much always use empty rather
than length == 0.
if(array)
is a bit different, because you're not specifically checking the length, but
if(container)
doesn't work in the general case, and stuff like
if(array || cond)
doesn't work. So, making
if(array)
be equivalent to
if(array.length != 0)
and
if(!array.empty)
rather than
if(array !is null)
may be a good idea, but it doesn't work in the general case. In the general
case, you're still going to have to choose between length == 0 and empty, and
I definitely think that empty is the correct choice, because it promotes good
habits, whereas length == 0 promotes bad habits. So, there's value in putting
empty in _object.d regardless of what happens with if.
Now, I find that I use enough other stuff in std.array, that it always gets
imported anyway, but I don't think that putting empty in _object.d is a bad
idea.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list