4 ms·
What’a the pros / cons with dispatch compared to the latter?
by Alifatisk 4y ago
What’a the pros / cons with dispatch compared to the latter?
- klodolph 4y agoWith the latter approach, you can save the bound method as a value. Depending on the way this is implemented, it can carry some additional overhead—because you create an object to represent the closure. The Objective-C dispatch, objc_msgSend, is very fast and allocates no memory. This is an implementation detail, however. I’d say that there’s a big difference between Python/Go and JS/C++. In Python, obj.method returns a bound method call—basically, a function that saves obj and passes it in as the first argument, and obj.method() has exactly the same semantics as x=obj.method;x(). In JS, obj.method returns an unbound method, and the equivalent to obj.method() is x=obj.method;x.call(obj). Objective-C has some nice semantics and a little bit of typing to help you out here.