KeyType

Takes a type which is an associative array and evaluates to the type of the keys in that associative array.

alias KeyType(V : V[K], K) = K

Examples

static assert(is(KeyType!(int[string]) == string));
static assert(is(KeyType!(string[int]) == int));

static assert(is(KeyType!(string[const int]) == const int));
static assert(is(KeyType!(const int[string]) == string));

struct S
{
    int i;
}

string[S] aa1;
static assert(is(KeyType!(typeof(aa1)) == S));

S[string] aa2;
static assert(is(KeyType!(typeof(aa2)) == string));

KeyType!(typeof(aa1)) key1 = S(42);
KeyType!(typeof(aa2)) key2 = "foo";

// Key types with indirections have their inner layers treated as const
// by the compiler, because the values of keys can't change, or the hash
// value could change, putting the associative array in an invalid state.
static assert(is(KeyType!(bool[string[]]) == const(string)[]));
static assert(is(KeyType!(bool[int*]) == const(int)*));

// If the given type is not an AA, then KeyType won't compile.
static assert(!__traits(compiles, KeyType!int));
static assert(!__traits(compiles, KeyType!(int[])));

See Also

Meta