operator == abstract method Null safety

bool operator ==(
  1. Object other
)
override

Test whether another object is equal to this function.

Function objects are only equal to other function objects (an object satisfying object is Function), and never to non-function objects.

Some function objects are considered equal by == because they are recognized as representing the "same function":

  • It is the same object. Static and top-level functions are compile time constants when used as values, so referring to the same function twice always yields the same object, as does referring to a local function declaration twice in the same scope where it was declared.
    void main() {
      assert(identical(print, print));
      int add(int x, int y) => x + y;
      assert(identical(add, add));
    }
    
  • The functions are same member method extracted from the same object. Repeatedly extracting ("tearing off") the same instance method of the same object to a function value gives equal, but not necessarily identical, function values.
    var o = Object();
    assert(o.toString == o.toString);
    
  • Instantiations of equal generic functions with the same types yields equal results.
     T id<T>(T value) => value;
     assert(id<int> == id<int>);
    
    (If the function is a constant and the type arguments are known at compile-time, the results may also be identical.)

Different evaluations of function literals never give rise to equal function objects. Each time a function literal is evaluated, it creates a new function value that is not equal to any other function value, not even ones created by the same expression.

var functions = <Function>[];
for (var i = 0; i < 2; i++) {
  functions.add((x) => x);
}
assert(functions[0] != functions[1]);

Implementation

bool operator ==(Object other);