What is the difference between 'is' and '==' in Dart?

前端 未结 2 1067
一生所求
一生所求 2021-01-15 16:40

Let\'s say I have:

class Test {
  void method() {
    if (T is int) {
      // T is int
    } 

    if (T == int) {
      // T is int
    }
  }
}


        
相关标签:
2条回答
  • 2021-01-15 17:24

    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).

    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.

    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).

    0 讨论(0)
  • 2021-01-15 17:25
    • identical(x, y) checks if x is the same object as y.

    • x == y checks whether x should be considered equal to 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).

    • x is T checks whether x has type T. 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.

    0 讨论(0)
提交回复
热议问题