【发布时间】:2015-11-18 11:22:37
【问题描述】:
我想创建一个与传递的枚举类型一起使用的泛型类,如下所示:
class Holder<E, T> { // I suppose to pass only enum types as E, how to limit them?
function new() {
mMap = new Map<EnumValue, T>();
}
function add(aKey:EnumValue, aValue:T) { // aKey is practically ANY enum value
mMap.set(aKey, aValue);
}
var mMap:Map<EnumValue, T>; // what type should I use as key? like E<EnumValue>
}
enum Keys {
A;
B;
}
enum Injection {
NOT_ALLOWED;
}
// first drawback
var holder = new Holder<Keys, String>();
holder.add(Keys.A, "This is A");
holder.add(Keys.B, "This is B");
holder.add(Injection.NOT_ALLOWED, "Not allowed by design!"); // it must not be allowed
// second drawback
var strange = new Holder<String, String>(); // it must not be allowed too
我从不在类声明中使用 E 类型,因为我不明白如何将枚举值传播回它的类型。而且我不能只在类定义中写Holder<Enum<E>,T> 来使用E 类型作为枚举值。
我应该怎么做才能将E 限制为枚举类型,然后只获取这种类型的参数?
【问题讨论】: