jesper
jesper

Reputation: 395

Testing for identical objects in Dart

According to the documentation, the function identical checks whether two references are to the same object.

With that in mind, I don't understand why the following is the case:

  int a = 1;
  int b = 1;
  print(identical(a, b)); // prints 'true'
  Map c = { 1: 'y' };
  Map d = { 1: 'y' };
  print(identical(c, d)); // prints 'false'

I'd expect both calls to return 'false'.

Upvotes: 1

Views: 202

Answers (1)

Sergey Romanovsky
Sergey Romanovsky

Reputation: 4554

identical compares references. a and b are references to a compile time literal 1. Thus they are identical.

Upvotes: 2

Related Questions