Dart:如何在运行时确定可为空的泛型类型?

Dart: how to determine nullable generic type at runtime?

我需要确定 通用类型 Stringboolintdouble 还是其他类型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();
}

控制台输出:

// 'T' is an int
// 'T' is not an int

是否有任何我不知道的检查可空类型的语法?我已经尝试过 int? 但它无法编译。

这是一个更具体的例子,基于 的方法。

注意,所以要区分Dartdouble/double?int/int?,我们必须先用floating-不能是 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
}

请注意,上述方法不适用于 voidNullNull 可以通过检查 T == Null 来处理,但是 T == void 不是有效语法(类似于 T == int?)。您可以通过将它们作为进行比较的通用函数的类型参数来解决这个问题,因此另一种方法是:

/// 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 除外)。

您可以创建函数来检查作为类型参数提供的两种类型是否等价。以下是一些示例:

/// 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 运算符,它将对象与类型进行比较。这就是为什么这里的所有函数都根据 S 创建一个已知类型的对象,并根据 T.

的类型检查它