Dart 为什么可以';是否在as表达式中使用t Object.runtimeType?
根据的Dart文档,字段的类型为Dart 为什么可以';是否在as表达式中使用t Object.runtimeType?,dart,Dart,根据的Dart文档,字段的类型为type。这让人困惑,因为我从编译器那里得到一个错误,抱怨这个字段不是类型 请参见此示例代码: final double first = 1.0; final int second = 2; final third = second as double; // works fine, unlike declaration below. assert(first.runtimeType == double); // true final fourt
type
。这让人困惑,因为我从编译器那里得到一个错误,抱怨这个字段不是类型
请参见此示例代码:
final double first = 1.0;
final int second = 2;
final third = second as double; // works fine, unlike declaration below.
assert(first.runtimeType == double); // true
final fourth = second as first.runtimeType;
最后一行抛出此编译时错误:
名称“first.runtimeType”不是类型,因此不能在“as”表达式中使用
示例代码显示了
first.runtimeType==double
,那么它不就是将\uuu作为first.runtimeType
等同于\uuuu作为double
?我认为它实际上很简单,runtimeType
仅在运行时可用,编译器无法对其进行静态分析。主持人注意-请在评论部分。