In Dart instances of a class could be treated as functions. To achieve this, the class should implement a call
method. This example
illustrates the usage of this language feature:
class F {
void call() {}
}
void g(void Function() f) {
f();
}
g(F()); // Here an instance of F is passed where the Function is expected
While this implicit tear off looks very concise, it has a serious drawback. This implicit conversion might happen accidentally, if you just happen
to have a call
method and by mistake passed an instance as a Function
. Moreover, such code is much harder to read and
understand as you don’t see any difference, between being passed as an object or as a function. Taking this all into account, this behavior could be
removed in the next versions of the language. Thus, to avoid confusion and potential compatibility problem, this is highly recommended to make the
passing of a call()
method explicit.