Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 794
Paul Milham Avatar asked Apr 20 '26 23:04

Paul Milham


1 Answers

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.

like image 81
Alexandre Ardhuin Avatar answered Apr 22 '26 14:04

Alexandre Ardhuin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!