【发布时间】:2014-02-19 05:28:44
【问题描述】:
在 Dart 中存在两种类型。
- 运行时类型
- 静态类型
这是Dart language specification中的证明:
null的静态类型是底部。
-
null的运行时类型为Null -
null的静态类型为bottom
这意味着 Dart 中的对象可以有两种类型。
一种称为static 的真实类型和一种称为runtime 的virtual 类型。
也就是说,null 的运行时类型不是bottom,而是常规类Null。
class Null {
factory Null._uninstantiable() {
throw new UnsupportedError('class Null cannot be instantiated');
}
/** Returns the string `"null"`. */
String toString() => "null";
}
但同时,此常规运行时类型 Null 的值可以分配给任何其他类型,因为 null 的真实(静态)类型是 bottom 类型。
Dart 中如何称呼这种技术?
类型替换还是别的什么?
附言
这个问题是关于值的静态类型,而不是关于使用类型注释声明的变量的静态类型。
这是因为null 不是变量,而是value 与static type 的bottom。
附言
非常奇怪的案例(至少对我来说)。
void main() {
Null _null;
String s = _null;
}
我收到警告:
A value of type 'Null' cannot be assigned to a variable of type 'String'
说的很对。但同时这也有效。
具有类型替换(静态和运行时)的奇特事物。
【问题讨论】:
-
“null 不能分配给字符串”警告似乎是一个错误(在分析器中或在规范中)。静态可确定的是,所有 Null 类型的值都可以分配给 String 类型的变量。
-
@lrn 我只是依靠事实。
标签: null dart virtual-machine dart-sdk bottom-type