[英]Dart: how to determine nullable generic type at runtime?
我需要在运行时确定泛型类型是String
、 bool
、 int
、 double
还是其他 class 。 对于可空类型,我没有找到一种方法:
class Foo<T> {
void foo() {
if (T == int) {
print("'T' is an int");
} else {
print("'T' is not an int");
}
}
}
void main() {
final foo = Foo<int>();
final bar = Foo<int?>();
foo.foo();
bar.foo();
}
控制台 output:
// 'T' is an int
// 'T' is not an int
是否有任何我不知道的语法来检查可空类型?,我已经尝试过使用int?
但它不编译。
这是一个更具体的示例,基于如何在 Dart NNBD 中检查泛型类型是否可以为空? .
注意,转译到JavaScript时,所有数字都是IEEE-754双精度浮点值,所以要区分Dart double
/ double?
和int
/ int?
,我们必须首先检查不能是int
的浮点文字。
void foo<T>() {
if (1.5 is T) {
if (null is T) {
print('double?');
} else {
print('double');
}
} else if (1 is T) {
if (null is T) {
print('int?');
} else {
print('int');
}
} else {
print('something else');
}
}
void main() {
foo<int?>(); // Prints: int?
foo<int>(); // Prints: int
foo<double?>(); // Prints: double?
foo<double>(); // Prints: double
foo<bool>(); // Prints: something else
}
请注意,上述方法不适用于void
或Null
。 Null
可以通过检查T == Null
来处理,但是T == void
不是有效的语法(类似于T == int?
)。 您可以通过将它们类型参数设置为进行比较的通用 function 来解决此问题,因此另一种方法是:
/// Returns true if T1 and T2 are identical types.
///
/// This will be false if one type is a derived type of the other.
bool typesEqual<T1, T2>() => T1 == T2;
void foo<T>() {
if (typesEqual<T, void>()) {
print('void');
} else if (typesEqual<T, Null>()) {
print('Null');
} else if (typesEqual<T, int>()) {
print('int');
} else if (typesEqual<T, int?>()) {
print('int?');
} else if (typesEqual<T, double>()) {
print('double');
} else if (typesEqual<T, double?>()) {
print('double?');
} else {
print('something else');
}
}
void main() {
foo<int?>(); // Prints: int?
foo<int>(); // Prints: int
foo<double?>(); // Prints: double?
foo<double>(); // Prints: double
foo<void>(); // Prints: void
foo<Null>(); // Prints: Null
foo<bool>(); // Prints: something else
}
我建议避免将Type
对象用于任何严重的事情(打印或dart:mirrors
除外)。
您可以创建函数来检查作为类型 arguments 提供的两种类型是否等效。 这里有些例子:
/// Whether two types are equivalent.
///
/// The types are equivalent if they are mutual subtypes.
bool equivalentTypes<S, T>() {
return _Helper<S Function(S)>() is _Helper<T Function(T)>;
}
class _Helper<T> {}
// Or alternatively:
bool equivalentTypes2<S, T>() {
S func(S value) => value;
return func is T Function(T);
}
/// Whether two types are the same type.
///
/// Uses the same definition as the language specification for when
/// two types are the same.
/// Currently the same as mutual subtyping.
bool sameTypes<S, T>() {
void func<X extends S>() {}
// Spec says this is only true if S and T are "the same type".
return func is void Function<X extends T>();
}
void main() {
print(equivalentTypes<int, int>());
print(equivalentTypes<int?, int?>());
print(equivalentTypes<int?, int>());
print(equivalentTypes2<int, int>());
print(equivalentTypes2<int?, int?>());
print(equivalentTypes2<int?, int>());
print(sameTypes<int, int>());
print(sameTypes<int?, int?>());
print(sameTypes<int?, int>());
}
该语言只有一个运算符用于将类型与任何内容进行比较,即is
运算符,它将 object 与类型进行比较。 这就是为什么这里的所有函数都创建一个依赖于S
的已知类型的 object 并根据依赖于T
的类型对其进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.