Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the implementation of call() in dart?

Tags:

callable

dart

I somehow found the call() method is on every function. Using this method I could change my if (callback != null) callback() to callback?.call().

So I tried to find the implementation and document of call(), but I couldn't. Is it just built-in method? What will be the implementation of this method? Function.apply() will be called inside it?

like image 405
wurikiji Avatar asked Nov 13 '19 08:11

wurikiji


1 Answers

All Dart functions (objects which has a function type rather than an class/interface type) have a call method.

The call method has the same function type as the function itself, and it behaves exactly the same when you call it. You could even say that calling a function is implicitly calling its call method. And, not by coincidence, the specification actually does say that: If you write the function invocation e1(e2, e3), then the compiler checks if e1 has call method, and if so converts it to the method invocation e1.call(e2, e3).

Other Dart objects may have a call method too. It's just a normal method for interface types, but if class C has a call method like int call(int x) => ..., and c has type C, then c(e2, e3) is also converted to c.call(e2, e3). It has to be a call method, not just a call getter returning a function.

like image 192
lrn Avatar answered Dec 02 '22 23:12

lrn