Associative-array .remove method returns void for non-existent keys

Michael Rynn michaelrynn at optusnet.com.au
Wed Jul 28 07:07:59 PDT 2010


On Tue, 27 Jul 2010 16:40:42 -0400, Andrej Mitrovic wrote:

> I screwed up the title, it returns void for both existent and
> non-existent keys.
> 
> Andrej Mitrovic Wrote:
> 
>> In TDPL page 116, for the associative method .remove it states:
>> 
>> "The remove method returns a bool that is true if the deleted key was
>> in the associative array, or false otherwise"
>> 
>> In this example the .remove method will return void regardless if the
>> key was found or not:
>> 
>> import std.stdio;
>> 
>> void main() {
>>     int[string] array = ["test":0, "test2":1];
>>     
>>     bool found = array.remove("test");
>>     bool notfound = array.remove("nothing");
>> }
>> 
>> Errors:
>> assoc_test.d(6): Error: expression array TOK44 "test" is void and has
>> no value assoc_test.d(7): Error: expression array TOK44 "nothing" is
>> void and has no value

Its funny about that, because I have created a compatible AA druntime 
implementation, which can be used through a template to access additional 
druntime functions. The template access to remove calls a new _aaDelNode 
which returns a bool. 
The rt.aaA druntime replacement still implements all the old rt.aaA 
calls, which work just the same, with the same AA object , so

  valueType[keyType] myAA;

  myAA.remove(keyTypeValue);   // compiler calls _aaDel , which is return 
void.

  import hash.druntime;

  hash.druntime.HashTable!( valueType[keyType] )  myAAT.

  bool  didIt = myAAT.remove(keyTypeValue); // was there before I called,
     template calls _aaDelNode, an additional function in the rt.aaA.

  There are a few other optional enhancements as well,
  including a HashSet template which stores keys only.

  The dmd compiler will still generate code for the older style AA, and 
being a built-in type the interface calls are probably really old parts 
of the code generator. One interface can be obtained from the other, so 
old code can mix with new.

    valueType[keyType] /* or auto */ myAA = myAAT.AA;

    
// adopt the older native interface object, as created by code generated 
by current compiler and work with the extras

	myAAT.AA(myAA);


To use this not officially approved, not reviewed, and apparently 
forgotten interface, (I submitted it as an enhancement request a while 
ago, and I presume everyones busy) the code is available (along with some 
other template implementations of hashtable / hashset, which can do just 
as well or better), from the dsource.org/projects/aa.  Replace the 
rt.aaA, recompile the druntime, (not tested on anywhere except windows, 
but its all written in D. If someone showed enthusiasm, I might be 
inspired to do more testing on other platforms) and use the hash.druntime 
module in the project for the template, and away you go..

Eventually something like it will be in a later druntime.

Michael.





More information about the Digitalmars-d mailing list