How i can clear Associative Arrays

Andrea Fontana nospam at example.com
Mon Apr 15 01:32:02 PDT 2013


On Sunday, 14 April 2013 at 06:50:08 UTC, gedaiu wrote:
> On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot 
> wrote:
>>> I think we should introduce a removeAll function for hashes. 
>>> Either
>>> through Druntime or through a UFCS function that we could put 
>>> in
>>> std.array or somewhere.
>>
>> How about .clear() for consistency with C++ containers?
>
> It looks nice... i am waiting for it :P

If you can't do myArray = null, you can define this:

void removeAll(T, K)(T[K] arr)
{
	foreach(k; arr.keys)
		arr.remove(k);
}

or this:

import std.traits;
void removeAll(T)(T arr) if (isAssociativeArray!T)
{
	foreach(k; arr.keys)
		arr.remove(k);
}

and then using UCFS you can write:

string[int] test;
test[10] = "hello";
test[20] = "world";

test.writeln;
test.removeAll;
test.writeln;


More information about the Digitalmars-d-learn mailing list