【问题标题】:Dart 2.7 generic extendsDart 2.7 通用扩展
【发布时间】:2020-03-21 06:31:17
【问题描述】:

我在将 dart 1.25 代码转换为 2.7 时遇到问题。 我的问题是泛型扩展约束。

在较旧的 dart 1.25 版本中,泛型类型在未指定时被理解为 num。

void main() {
  CustomType customType = new CustomType();// no T specified
}

class CustomType<T extends num> {
  CustomType() {
    print(T is num);//> why is this false ?
  }
}

为什么不是这样?

【问题讨论】:

    标签: generics dart


    【解决方案1】:

    您在比较类时使用了错误的运算符。使用:

    • == 比较 2 个类是否相等
    • is 检查对象是否为类的实例。
    void main() {
      CustomType customType = new CustomType();// no T specified
    }
    
    class CustomType<T extends num> {
      CustomType() {
        print(T);        // num
        print(T == num); // true
        print(1 is num); // true
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2020-02-05
      • 2021-10-09
      相关资源
      最近更新 更多