huntlabs / hunt

A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Apache License 2.0
95 stars 15 forks source link

Add keyExists(T) or hasKey(T) method for Array class? #49

Closed zoujiaqing closed 5 years ago

zoujiaqing commented 5 years ago

code:

string[int] users;

users[10001] = Brian;
users[10002] = Heromyth

if (users.hasKey(10001))
{
    // OK
}

if (users.keyExists(10002))
{
    // OK
}
Heromyth commented 5 years ago
bool hasKey(K, V)(V[K] arr, K key) {
    auto v = key in arr;
    return v !is null;
}

unittest {
    string[int] arr;
    arr[1001] = "1001";
    arr[1002] = "1002";

    assert(arr.hasKey(1001));
    assert(!arr.hasKey(1003));
}