Search code examples
objective-clanguage-agnosticlanguage-designsmalltalkmessage-passing

Is message passing in small talk and ObjectiveC same as calling method with value types (structs) in argument?


According to wikipedia the difference between message passing and calling a method is "In message passing, each of the arguments has to have sufficient available extra memory for copying the existing argument into a portion of the new message" while in the method call only the address of the arguments is passed.

How is message passing different than calling a regular method all arguments of which are structs or value type i.e. they all have to be pushed entirely in stack for the callee to be able to use them?


Solution

  • Once the actual function call is made, there is no difference. The difference between message passing and calling a method is in the linking. For languages like c and c++ the function calls are linked at compile time with the linker (except with virtual functions which requires some runtime support). With languages that use a messaging system like objective-c and smalltalk, you cannot guarantee what functions will be run until runtime. The runtime determines if an object actually implements the function represented in the message. If it doesn't implement it, the class will usually either forward the message onto another object, or throw an exception. However, if the class does implement it, the runtime determines the address of the actual function, and calls it in the exact same manner as c (pushing the arguments and the return address onto the stack).

    Overall, a message is the same thing as calling a method directly, except the runtime finds the exact function to be called instead of it being linked at compile time.