Dart has both:
==
andidentical()
. 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()
.
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.)
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
.
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:
==
to Java's equal()
method and identical()
to Java's equality test ==
.But that now seems wrong. Anyone know what is going on?
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.)
Here is an excerpt:
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: ...
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.