Saturday, June 28, 2008

Subjective-C

Reading through primers for Objective-C, I'm struck by the use of new names for existing concepts. For example:
A protocol declares methods that can be implemented by any class. Protocols are not classes themselves. They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes, your class is said to
conform to that protocol.
Interesting, so a protocol is just an interface that other objects implement. In other parts of the world, we call such an interface an interface.
A class in Objective-C can declare two types of methods: instance methods and class methods. An instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class. Class methods, by comparison, do not require you to create an instance, but more on that later.
So instead of declaring methods static, we now have somewhat less expressive notations of "-" for instance methods and "+" for class methods. Class methods are different than static methods, Apple tells me, but why do we have to resort to "+"?
When you want to call a method, you do so by "messaging” the corresponding object. The message in this case is the method signature, along with the parameter information the method needs. All messages you send to an object are dispatched dynamically, thus facilitating the polymorphism behavior of Objective-C classes. In other words, if a subclass defines a method with the same signature as one of its parent classes, the subclass receives the message first and can opt to forward the message (or not) to its parent.
So the "message" you send (with quotes) is equivalent to calling a method (without quotes). Also, what part of polymorphic inheritance isn't already part of other OOPLs, like Java?

Final gripe that may just be a n00b question: If the "messaging" syntax is represented by [object :parameter], how do you do method chaining?
[[[object method1:parameter1] method2:parameter2] method3:parameter3]?
That looks ugly.

Still wondering why we need another language to program on a different platform but maybe I'll change my mind once I delve deeper. For now, Apple seems to be continuing to do its thang of making a big deal out of existing concepts. XCode does "Data Tips". Great. What other decent IDE doesn't?

2 comments:

Chris Bouchard said...

I'd first like to point out that Objective-C was created about 10 years before Java popularized the term interface. In fact, Java interfaces were inspired by Objective-C.

Also, messaging is different from method calling because in Objective-C, you won't get an error if you send a message to an object that doesn't implement it, while a Java program won't compile if you call a non-existent method.

C++ used the static keyword to create class methods, and Java copied that. But they're really called class methods. I don't know why Objective-C decided to use + and -, but it can't use static because it's a super-set of C.

The message style (obj methArg1: a arg2: b) is from Smalltalk, one of the languages Objective-C is based on. I assume they added the [...] around it so it would be separated from the surrounding C code.

Sorry to come in and just start spouting my head off, but I figured you might like to have some answers to your questions.

Objective-C on Wikipedia

Me said...

I fully expected to be (possibly) wrong on all accounts as I learned more but it's nice to have somebody do the work for me ;)

Problem is though I still don't understand the advantage of Objective C yet. I understand the historical influences behind the current incarnation of Objective C but history doesn't necessarily equal Good. I feel like I shouldn't have to dig too deep to understand the expressive potential of a language. So far, Objective-C as a language (separate from libraries) doesn't have me convinced.

Yet.