Testing functions for equality

Here’s an example of testing top-level functions, static methods, andinstance methods for equality:

  1. void foo() {} // A top-level function
  2. class A {
  3. static void bar() {} // A static method
  4. void baz() {} // An instance method
  5. }
  6. void main() {
  7. var x;
  8. // Comparing top-level functions.
  9. x = foo;
  10. assert(foo == x);
  11. // Comparing static methods.
  12. x = A.bar;
  13. assert(A.bar == x);
  14. // Comparing instance methods.
  15. var v = A(); // Instance #1 of A
  16. var w = A(); // Instance #2 of A
  17. var y = w;
  18. x = w.baz;
  19. // These closures refer to the same instance (#2),
  20. // so they're equal.
  21. assert(y.baz == x);
  22. // These closures refer to different instances,
  23. // so they're unequal.
  24. assert(v.baz != w.baz);
  25. }