generics - Dart 2.7 通用扩展
问题描述
我在将 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 ?
}
}
为什么不是这样?
解决方案
您使用了错误的运算符来比较类。利用:
==
比较 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
}
}
推荐阅读
- azure-devops - Visual Studio 测试步骤成功完成后未报告代码覆盖率结果
- python - 分数函数返回未减少的分数
- postgresql - 获取旧版本 JDBC jar 的文档
- batch-file - ftp.exe 找不到作为环境变量值的文件夹
- swift - Swift:使用闭包添加 2 个数字的程序
- javascript - 过滤数组 (JS)
- matlab - MATLAB - 将不同的数组/矩阵连接到更大的矩阵
- django - 添加带有 label_tag 的 Font Awesome 图标
- docker - 如何并行化 docker run?
- sql-server - 具有两个外键列的 Sql 条件