What is the implementation of call() in dart?

馋奶兔 提交于 2020-08-08 06:15:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/58833369/what-is-the-implementation-of-call-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!