枚举当索引

enum Foo {
one,
two
}

struct struct_t {}

struct array {
static private struct_t[Foo.max + 1] content;
static struct_t opIndex(Foo idx) { return content[cast(int) idx]; }
//重载.
}

void main() {
struct_t a = array[Foo.one];
}

然后,如果想要​​更简单的重用​​​,可以​​将其泛化​​.

struct enum_array(Key, Value) {
static private struct_t[Key.max + 1] content;
static Value opIndex(Key idx) { return content[cast(int) idx]; }
}

alias array = enum_array!(Foo, struct_t);