我正在尝试确定是否dynamic
函数的参数实际上是一个int
or a 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
在浏览器中,两者之间没有区别int
and double
。
JavaScript 不提供任何此类区别,为此目的引入自定义类型会对性能产生很大影响,这就是为什么没有这样做。
因此,对于 Web 应用程序来说,通常最好坚持使用num
.
您可以使用以下命令检查值是否为整数:
var val = 1.0;
print(val is int);
prints true
这只表明小数部分是或不是0
.
在浏览器中,值没有附加类型信息,因此is int
and is double
似乎只是检查数字是否有小数部分,并仅根据它来决定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)