What about putting array.empty in object.d?

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 21 12:46:12 PDT 2012


On Wed, 21 Mar 2012 14:33:58 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> 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.

But we are specifically talking about arrays, not containers in general.   
Containers in general are not defined by the language.

> 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.

I think you may misunderstand the proposal.  if(array) translating to  
if(array.length) is *only* for arrays, not for general types.

I don't see why defining empty in object.d is necessary for things that  
don't involve ranges at all.

Any time you import a container, it's going to import std.range, which  
publicly imports std.array.

Bottom line: if you don't care about ranges, if(array) works just fine  
(checks if length != 0).  If you care about ranges, you are going to be  
importing std.range and therefore std.array, and array.empty works just as  
well.

As an bonus this gets rid of the controversial behavior of if(array)  
translating to if(array.ptr).

-Steve


More information about the Digitalmars-d mailing list