[英]What is the difference between 'is' and '==' in Dart?
Let's say I have:假设我有:
class Test<T> {
void method() {
if (T is int) {
// T is int
}
if (T == int) {
// T is int
}
}
}
I know I can override ==
operator but what's the main difference between ==
and is
in Dart if I don't override any operator.我知道我可以覆盖==
运算符,但如果我不覆盖任何运算符, ==
和 Dart 之间的主要区别is
什么。
Edit:编辑:
Say I have说我有
extension MyIterable<T extends num> on Iterable<T> {
T sum() {
T total = T is int ? 0 : 0.0; // setting `T == int` works
for (T item in this) {
total += item;
}
return total;
}
}
And when I use my extension method with something like:当我使用我的扩展方法时:
var addition = MyIterable([1, 2, 3]).sum();
I get this error:我收到此错误:
type 'double' is not a subtype of type 'int' “double”类型不是“int”类型的子类型
identical(x, y)
checks if x
is the same object as y
. same identical(x, y)
检查x
是否与y
相同 object 。
x == y
checks whether x
should be considered equal to y
. x == y
检查是否应将x
视为等于y
。 The default implementation for operator ==
is the same as identical()
, but operator ==
can be overridden to do deep equality checks (or in theory could be pathological and be implemented to do anything). operator ==
的默认实现与 same identical()
,但operator ==
可以被覆盖以进行深度相等检查(或者理论上可能是病态的并被实现以做任何事情)。
x is T
checks whether x
has type T
. x is T
检查x
是否具有类型T
。 x
is an object instance . x
是一个object 实例。
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 MyClass
是false
因为左侧是type ,而不是MyClass
的实例。 ( MyClass is Type
would be true
, however.) ( MyClass is Type
将是true
,但是。)
In your code, T is int
is incorrect because both sides are types.在您的代码中, T is int
不正确,因为双方都是类型。 You do want T == int
in that case.在这种情况下,您确实需要T == int
。 Note that T == int
would check for an exact type and would not be true if one is a derived type of the other (eg int == num
would be false).请注意, T == int
将检查一个确切的类型,如果一个是另一个的派生类型(例如int == num
将是错误的),则不会为真。
Basically, == is equality operator and "is" is the instanceof operator of Dart (If you come from Java background, if not, it basically tells you if something is of type something).基本上, == 是相等运算符, “is”是 Dart 的 instanceof 运算符(如果您来自 Java 背景,如果不是,它基本上会告诉您某些东西是否属于某种类型)。
Use == for equality, when you want to check if two objects are equal.当您想检查两个对象是否相等时,使用 == 表示相等。 You can implement the == operator (method) in your class to define on what basis do you want to judge if two objects are equal.您可以在 class 中实现 == 运算符(方法),以定义您要判断两个对象是否相等的基础。
Take this example:举个例子:
class Car {
String model;
String brand;
Car(this.model, this.brand);
bool operator == (otherObj) {
return (otherObj is Car && other.brand == brand); //or however you want to check
//On the above line, we use "is" to check if otherObj is of type Car
}
}
Now you can check if two cars are "equal" based on the condition that you have defined.现在您可以根据您定义的条件检查两辆车是否“相等”。
void main() {
final Car micra = Car("Micra", "Nissan");
print(micra == Car("Micra", "Nissan")); // true
print(micra is Car("Micra", "Nissan")); // true
}
Hence, == is something you use to decide if two objects are equal, you can override and set it as per your expectations on how two objects should be considered equal.因此, == 是您用来确定两个对象是否相等的东西,您可以根据您对如何将两个对象视为相等的期望来覆盖和设置它。
On the other hand, "is" basically tells you if an instance is of type object (micra is of type Car here).另一方面,“is”基本上告诉您实例是否为 object 类型(此处的 micra 为 Car 类型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.