What messages give you in Objective-C against simple method calls in C # and Java?

I am learning Objective C's C # / Java developer. At first, I assumed that “messages” was just a different name for method calls, so:

[person jumpInTheAir]; 

would just be Objective-C syntax for writing

 person.jumpInTheAir(); 

But now I have read here and here and in various other places that the concepts do not really coincide and can have different forms of behavior / advantages. However, I'm still not sure why the language developers chose the messaging system in a more direct method invocation system, like in C # and Java.

What benefits do messages in the Objective-C programming language bring?

+4
source share
2 answers

At first, I suggested that “messages” were just a different name for method calls,

Yes and no. Messages are Smalltalk and Objective-C terms for method calls. The fact is that not only the terminology is different, but also the real implementation. There are 8 different possible combinations to match terminology, syntax, and implementation, for example:

 +-------------------+----------------------+--------------------+ | 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 that the language designer chooses is simply a matter of taste.

I'm still not sure why the language developers chose a messaging system in a more direct call system

Since it has some advantages, such as interposition and introspection of runtime, you can query and change the behavior of classes, methods, and objects at runtime. In the implementation of Objective-C, this was done in such a way that it is very cheap, it has almost no overhead.

+3
source

H2CO3 indicated in its response that there are three things that I could talk about when I say “messages”: Terminology , Syntax, and Implementation .

The terminology and syntax comes down to personal tastes, but implementation (static / dynamic binding) has some real consequences.

Pros:

Minuses:

  • It can be argued that an additional layer adds complexity to both syntax and use.
  • Dynamic linking adds some overhead compared to the static type, but this is rarely a bottleneck, and there seem to be ways to get around the overhead if that is really important.
0
source

All Articles