问题描述
飞镖兼有:
- 相等运算符
==
和 - 一个名为
identical()
的顶级函数.
- an equality operator
==
and - a top-level function named
identical()
.
通过语法选择,感觉自然要比 identical()
更频繁地使用Dart的 ==
运算符,我很喜欢.实际上,关于平等的部分://www.dartlang.org/articles/idiomatic-dart"rel =" nofollow noreferrer>惯用飞镖指出:实际上,您几乎不需要使用 identical()
.
By the choice of syntax, it feels natural to want to use Dart's ==
operator more frequently than identical()
, and I like that. In fact, the Section on Equality of the Idiomatic Dart states that "in practice, you will rarely need to use" identical()
.
在最近对时使用 identical()
而不是 ==
模型已达到稳定状态.(我想,出于效率考虑,对于大型模型,这可能是有道理的.)
In a recent answer to one of my questions concerning custom filters, it seems that Angular Dart favors use of identical()
rather than ==
when trying to determine whether changes to a model have reached a steady state. (Which can make sense, I suppose, for large models for reasons of efficiency.)
这使我开始考虑 int
的身份,因此我在 int
上编写了一些 identical()
的测试.虽然我希望小 int
的可能会被"interned/cached"(例如类似于 Java的令我惊讶的是,我似乎无法生成两个相等但不相同的
int
.对于 double
,我得到类似的结果.
This got me to thinking about identity of int
's and so I wrote some tests of identical()
over int
s. While I expected that small int
s might be "interned/cached" (e.g. similar to what is done by Java's Integer.valueOf()
), to my surprise, I can't seem to generate two int
s that are equal but not identical. I get similar results for double
.
int
和 double
值是否被阻止/缓存?还是 identical()
会特别对待它们?来自Java背景,我曾经把Dart等同于:
Are int
and double
values being interned/cached? Or maybe identical()
is treating them specially? Coming from a Java background, I used to equate equate Dart's:
-
==
到Java的equal()
方法和 -
identical()
到Java的相等性测试==
.
==
to Java'sequal()
method andidentical()
to Java's equality test==
.
但是现在看来是错误的.有人知道发生了什么吗?
But that now seems wrong. Anyone know what is going on?
推荐答案
好像我发布得太快了.我偶然发现了Dart Issue 13084:规范说的same(1.0,1)是是的,即使它们具有不同的类型也是如此,这使我进入了 语言规范 .(我之前曾在规范中搜索过相等性,但没有对象标识.)
Seems like I posted too quickly. I just stumbled on Dart Issue 13084: Spec says identical(1.0, 1) is true, even if they have different types which led me to the Dart section on Object Identity of the language spec. (I had previously search for equality in the spec but not object identity.)
这是节选:
The predefined dart function identical() is defined such that identical(c1, c2) iff:
- c1 evaluates to either null or an instance of
bool and c1 == c2, OR
- c1 and c2 are instances of int and c1 == c2, OR
- c1 and c2 are constant strings and c1 == c2, OR
- c1 and c2 are instances of double and one of the following holds: ...
,还有更多处理列表,映射和常量对象的子句.有关完整的详细信息,请参见语言规范.因此, identical()
不仅仅是一个简单的引用相等性测试.
and there are more clauses dealing with lists, maps and constant objects. See the language spec for the full details. Hence, identical()
is much more than just a simple test for reference equality.
这篇关于Dart int和double被拘禁了吗?是否由same()特别处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!