Hacker News new | ask | show | jobs
by tedunangst 5970 days ago
Except it seems to be missing all the features that make Objective-C "Objective-C" in my mind. How do you implement method missing/message forwarding in Java? Oh, that's right, your code doesn't even compile.
1 comments

Too be fair, message forwarding is a pain in the ass in ObjC as well.
It's not really that hard, is it? You just have to implement methodSignatureForSelector and forwardInvocation.

Compare to Java where it's not even possible to forward messages determined at runtime, you have to generate stubs before compiling. AFAIK.

You have to have a method to forward to that accepts the right number of arguments of the right type. It's a far cry from overriding methodNotFound. I tried to recreate the ActiveRecord find style in SimpleData (http://github.com/briancollins/SimpleData) and it is just so hackish.
Or just a method which takes a NSInvocation object...
Interesting revelation, thanks!
Sure you can ... object.sendMessage(messageName) ;) Or for already built classes ... see java.lang.reflect.Method

Personally if I were to choose, I'd trade some syntactic sugar for a decent garbage-collector. But that's just me :)

Message forwarding is not syntactic sugar!
It is. In Objective C: [obj doFoo] would be analogous to this in a Java-like language: MethodSpec s = new MethodSpec("doFoo"); (obj.hasMethod(ms) ? obj.performMethod(ms) : obj.handleMissingMethod(ms)

However, people associate "syntactic sugar" with a connotation of not being a significant difference. Many very useful tools in programming are merely syntactic sugar. For, while, and similar control flow structures are just syntactic sugar over either continuations or gotos.

Message passing OO is syntactic sugar. As evidence, look at the object system the Gtk folks made for themselves in C, or the various object systems Perl programmers have made for themselves.

The example of object systems in Perl illustrates another point about syntactic sugar: having a language which makes it possible for programmers to add their own syntactic sugar within the language is incredibly powerful. Moose is a great system, and Perl's flexible syntax is what makes it possible(although I don't really know Perl well enough to know how it's implemented, its syntax is very pleasant). CLOS is another example of the power of languages which allow programmers to add their own syntactic sugar.

Syntactic sugar can be very good. Message forwarding not only is a very useful piece of syntactic sugar, but also allows the creation of certain types of very convenient syntactic sugar to a language.

Objective C 2.0's garbage collector works just fine tyvm
... but not on the iphone, sadly.