Reputation: 9509
I'm a C#/Java developer learning Objective C. At first I assumed "messages" were just a different name for method calls, so:
[person jumpInTheAir];
would just be the Objective C syntax for writing
person.jumpInTheAir();
But now I've read here and here and various other places that the concepts are actually not the same and can have different behaviours/advantages. However I'm still uncertain why the language designers would choose a messaging system over a more direct method calling system as in C# and Java.
What advantages do messages bring to the Objective C programming language?
Upvotes: 4
Views: 467
Reputation: 9509
H2CO3 pointed out in his answer that there are three things I might be referring to when I say "messages": Terminology, Syntax and Implementation.
Terminology and Syntax are down to personal taste, but implementation (static/dynamic binding) has some real consequences.
Pros:
Cons:
Upvotes: 0
Reputation:
At first I assumed "messages" were just a different name for method calls,
Yes and no. "Messages" are the Smalltalk and Objective-C terminology for method calls. The thing is, it's not only the terminology that differs, but the actual implementation too. There are 8 different possible combinations for matching the terminology, the syntax and the implementation, like this:
+-------------------+----------------------+--------------------+
| terminology | syntax | implementation |
+-------------------+----------------------+--------------------+
| "method call" | Simula (o.method()) | static binding | non-virtual C++ methods
+-------------------+----------------------+--------------------+
| "method call" | Simula | dynamic binding |
+-------------------+----------------------+--------------------+
| "method call" | Smalltalk ([o meth]) | static binding |
+-------------------+----------------------+--------------------+
| "method call" | Smalltalk | dynamic binding |
+-------------------+----------------------+--------------------+
| "message passing" | Simula | static binding |
+-------------------+----------------------+--------------------+
| "message passing" | Simula | dynamic binding |
+-------------------+----------------------+--------------------+
| "message passing" | Smalltalk | static binding |
+-------------------+----------------------+--------------------+
| "message passing" | Smalltalk | dynamic binding | Objective-C
+-------------------+----------------------+--------------------+
The combination the language designer choses is just a matter of taste.
I'm still uncertain why the language designers would choose a messaging system over a more direct method calling system
Because it has some advantages, such as runtime interposition and introspection -- one can query and modify the behavior of classes, methods and objects at runtime. In the implementation of Objective-C, this have been done in a way such that it is very cheap, it has almost no overhead.
Upvotes: 3