functiondartequalitymember

Dart member function equality rules


I'm trying to test for equality of a member function of a class. Here is a small sample:

void main() {
  var foo = new Foo();    

  if (foo.someFunc == foo.someFunc)
    print("foo.someFunc == foo.someFunc");
  else
    print("foo.someFunc != foo.someFunc");
}    

class Foo {
  someFunc() {
  }
}

This prints "foo.someFunc != foo.someFunc". The equality operator here should be testing if the functions are the same object in memory (and it seems like they should be.) I also tried using identical(foo.someFunc, foo.someFunc), but got the same result. Why doesn't the equality operator return true in this case?


Solution

  • This is explained in the dart docs.

    Basically, you create a different closure each time you use foo.someFunc. That's why they are not equals.