簡體   English   中英

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();
}

控制台 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
}

請注意,上述方法不適用於voidNull 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM