Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What’a the pros / cons with dispatch compared to the latter?


With 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.


objc_msgSend does allocate memory - for the IMP caches, and the class initialization costs (and calling +initialize) the first time you message a class.


Sure. It allocates zero heap memory per call, amortized.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: