一个类型整数的值,double&同时num?
我有以下表达式:
const value = 10 / 2;
在VS代码中,如果我徘徊在value
上,它显示了它的类型是double
,这是有道理的。但是,如果我运行以下代码行,它们都将评估为true
:
print(value is num); // true
print(value is double); // true
print(value is int); // true
num
是超级类,并且double
& int
是同级类。兄弟姐妹类型的价值如何&超级类型同时?
I have the following expression:
const value = 10 / 2;
In VS Code, if I hover over value
, it shows me its type is double
, which makes sense. But if I run the following lines of code, they all evaluate to true
:
print(value is num); // true
print(value is double); // true
print(value is int); // true
num
is the super class, and double
& int
are sibling classes. How come a value be of the sibling type & the super type simultaneously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在为网络编译,并在浏览器中运行。
DARTPAD为您做到这一点,如果您使用Web编译器(DART2JS或Dev-Compiler)编译程序,则会获得相同的效果。
在为Web编译DART时,将所有数字汇编为JavaScript号码,因为这是获得有效执行的唯一方法。
JavaScript没有整数作为单独的类型。所有JavaScript号码均为IEEE-754双精度浮点号,Dart称为
double
s。因此,当您具有
10/2
之类的数字,该数字将在本机飞镖中创建double
时,它将在网络上创建JavaScript编号5.0。整数文字5
也是如此。浏览器中只有一个“ 5”数字,因此整数和双重计算都创建了相同的数字。为了使其与类型系统一致,这意味着“ 5”是实现
int
和double
接口的值。像5.5
的分数值仅实现double
。本地运行时,整数和双打是单独的类型。
这全是在飞镖语言之旅中描述的。
You are compiling for the web, and running in a browser.
DartPad does that for you, and you get the same effect if you compile your program using the web compilers (dart2js or the dev-compiler).
When compiling Dart for the web, all numbers are compiled to JavaScript numbers, because that's the only way to get efficient execution.
JavaScript does not have integers as a separate type. All JavaScript numbers are IEEE-754 double precision floating point numbers, what Dart calls
double
s.So, when you have a number like
10 / 2
, which would create adouble
in native Dart, it creates the JavaScript number 5.0 on the web. So does the integer literal5
. There is only one "5" number in the browser, so both integer and double computations create the same number.To make that be consistent with the type system, it means that "5" is a value which implements both the
int
and thedouble
interface. A fractional value like5.5
only implementsdouble
.When running natively, integers and doubles are separate types.
This is all described in the Dart language tour.
尝试
输出是:
的结果。
try
The output is:
That is expected result.