【问题标题】:What is the difference between using the is-operator and using the run-time type in Dart在 Dart 中使用 is-operator 和使用运行时类型有什么区别
【发布时间】:2020-01-28 15:54:48
【问题描述】:

我通过dart codelab for iterables 工作并偶然发现了这个代码片段:

class EmailAddress {
  String address;

  EmailAddress(this.address);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
(a)          other is EmailAddress &&
(b)              runtimeType == other.runtimeType &&
                 address == other.address;
....

(a)行和(b)行有什么区别?对我来说,他们似乎也在做同样的事情。 或者更一般地问:使用 is-operator 和使用对象的 runtimeType 属性来检查 dart 中的运行时类型有什么区别?

【问题讨论】:

    标签: dart typechecking runtime-type


    【解决方案1】:

    aSet is Iterable - 这是true

    aSet.runtimeType == Iterable这是false

    所以 is 检查处理子类。

    此外,我们真的建议您避免使用runtimeType。特别是在编译为 JavaScript 时。它真的会炸毁你编译的应用程序的大小。

    我将在那个 codelab 上打开一个问题!

    【讨论】:

    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2019-08-02
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多