Define .empty property for hashes?

Ali Çehreli acehreli at yahoo.com
Fri Mar 2 13:21:00 PST 2012


On 03/02/2012 01:08 PM, Andrej Mitrovic wrote:
 > Is there a reason why there's no .empty property for hashes? std.array
 > defines it for arrays, and range types must have it defined.

Yes, empty is a part of the InputRange interface. Slices are InputRange 
ranges but associative arrays are not. Unless they also provide front, 
which would returns a tuple of key and value I guess, and popFront() 
which would remove that key and value.

But things get interesting again because implementing empty, front, and 
popFront() on the AA interface would conflate the collection with a range.

A better approach might be to continue using byKey() and byValue(), 
which are ranges and follow the recent proposal of byPair(). (I can't 
locate the thread for that. (?))

 > But
 > hashes are left out even though they define .length. This could be put
 > in std.array:
 >
 > @property bool empty(T)(in T a)
 >      if (isAssociativeArray!T)
 > {
 >      return !a.length;
 > }
 >
 > I can't mark it as neither pure, nothrow, nor safe. I guess that's an
 > implementation detail at this point.

Ali



More information about the Digitalmars-d-learn mailing list