Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.


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'

like image 736
iDecode Avatar asked May 02 '20 02:05

iDecode


People also ask

What is the IS operator used for in Dart?

The operators are special symbols that are used to carry out certain operations on the operands. The Dart has numerous built-in operators which can be used to carry out different functions, for example, '+' is used to add two operands. Operators are meant to carry operations on one or two operands.

What does ~/ mean in Dart?

~/ Divide, returning an integer result. and ~/= integer division and assignment.

What does => mean in flutter?

From the documentation: For functions that contain just one expression, you can use a shorthand syntax. The => expr syntax is a shorthand for { return expr; } . The => notation is sometimes referred to as arrow syntax.


2 Answers

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

like image 103
jamesdlin Avatar answered Oct 13 '22 03:10

jamesdlin


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

like image 39
Yudhishthir Singh Avatar answered Oct 13 '22 03:10

Yudhishthir Singh