-
identical(x, y)
checks ifx
is the same object asy
. -
x == y
checks whetherx
should be considered equal toy
. The default implementation foroperator ==
is the same asidentical()
, butoperator ==
can be overridden to do deep equality checks (or in theory could be pathological and be implemented to do anything). -
x is T
checks whetherx
has typeT
.x
is an object instance.
class MyClass {
MyClass(this.x);
int x;
@override
bool operator==(dynamic other) {
return runtimeType == other.runtimeType && x == other.x;
}
@override
int get hashCode => x.hashCode;
}
void main() {
var c1 = MyClass(42);
var c2 = MyClass(42);
var sameC = c1;
print(identical(c1, c2)); // Prints: false
print(identical(c1, sameC)); // Prints: true
print(c1 == c2); // Prints: true
print(c1 == sameC); // Prints: true
print(c1 is MyClass); // Prints: true
print(c1 is c1); // Illegal. The right-hand-side must be a type.
print(MyClass is MyClass); // Prints: false
}
Note the last case: MyClass is MyClass
is false
because the left-hand-side is a type, not an instance of MyClass
. (MyClass is Type
would be true
, however.)
In your code, T is int
is incorrect because both sides are types. You do want T == int
in that case. Note that T == int
would check for an exact type and would not be true if one is a derived type of the other (e.g. int == num
would be false).