Testing membership in associative array

Lettever real at email.com
Mon Mar 4 23:49:46 UTC 2024


I am trying to create a function that tests membership in nested 
associative array, however the function I create below, only 
accepts keys of the same type and if given keys of different 
types it does not compile, help would be appreciated.
This is a example of what Im talking about.
```d
import std.stdio;
void main()
{
     int[int][int] aa1;
     aa1[1][2] = 3;
     writeln(contains(aa1, 1, 2));   //returns true as expected
     int[int][string] aa2;
     aa2["1"][2] = 3;
     writeln(contains(aa2, "1", 2)); //does not compile because of 
this line
}
bool contains(M, K...)(M map, K keys)
{
     foreach(key; keys)
         if(key in map)
             return contains(map[key], keys[1 .. $]);
         else
             return false;
     return true;
}
```


More information about the Digitalmars-d-learn mailing list