【发布时间】:2017-10-12 10:39:42
【问题描述】:
我正在尝试确定函数的 dynamic 参数是否真的是 int 或 double 并且我发现了令人惊讶的行为(至少对我而言)。
谁能解释一下这个输出(在 dartpad 上生成)?
foo(value) {
print("$value is int: ${value is int}");
print("$value is double: ${value is double}");
print("$value runtimetype: ${value.runtimeType}");
}
void main() {
foo(1);
foo(2.0);
int x = 10;
foo(x);
double y = 3.1459;
foo(y);
double z = 2.0;
foo(z);
}
输出:
1 is int: true
1 is double: true
1 runtimetype: int
2 is int: true
2 is double: true
2 runtimetype: int
10 is int: true
10 is double: true
10 runtimetype: int
3.1459 is int: false
3.1459 is double: true
3.1459 runtimetype: double
2 is int: true
2 is double: true
2 runtimetype: int
【问题讨论】:
-
似乎
runtimeType适应了值。z += 0.1; foo(z);给2.1 runtimetype: double -
你是在虚拟机上运行还是通过 dart2js 运行?
-
@SethLadd 这些结果是在 dartpad 上获得的,但我们在 Chrome 中也看到了奇怪的东西(所以我认为 dart2js 参与了)。