Does the message system in Objective C rely on the kernel message system?
I don't really understand how ObjC can efficiently rely on messages. Does this mean a kernel such as mach has to be designed with this idea ?
Objective C seems like a good language, but I can't find it easier to grasp than C++.
How is a message system built in a language an advantage 开发者_StackOverflow中文版?
The messaging in the Objective-C is not directly related to the messaging in the mach kernel. It's implemented as a highly-tuned function called objc_msgSend
. The call
[obj messageWithX:x andY:y];
is translated to the C call
objc_msgSend(obj,@selector(messageWithX:andY:),x,y);
objc_msgSend
is written directly in assembly language to be maximally optimized. It resolves the method call dynamically in a very efficient way. This blog post would be a good place to start understanding how it is implemented.
Whether it's easier or harder to grasp than C++ will depend on your background and your taste. The flavors of object-orientedness supported by C++ and Objective-C are quite different, and so it's difficult to compare.
Objective-C's messaging system is highly dynamic, and most of the decision can be done at the run time. C++'s member function invocation system is more static, and tries to do as much as possible at the compile time.
One merit of the dynamical nature of Objective-C is that you can change the class system at run time, but that's not necessarily related to the messaging nature. For example, the messaging system in mach doesn't do that, as far as I understand.
One feature directly related to the messaging nature is that an object can capture a message which it does not understand, and then forward it to other objects. This can be done in mach: the receiver of a message can either be in the kernel space, in the user space, or even in another machine, and the sender of the message doesn't need to care about it. This adds more flexibility in designing the kernel. (But this feature is not used in the mach kernel in OS X.)
Objective-C messaging is not built into the kernel, it is build into the Objective-C run-time.
Most of the magic is done in a function called objc_msgSend()
. If you write a Objective-C code like this:
[myObject doStuffWith:something];
The compiler will actually generate the same code as if you had typed this:
objc_msgSend(myObject, @selector(doStuffWith:), something);
The implementation of of objc_msgSend()
is quite fast and smart. Fast by caching frequently used selector, and smart in that it allows for implementation resolution as late as possible. What objc_msgSend()
in practice do is a hash lookup for the selector to find an actual implementation.
One advantage that you have here is that if an implementation for a method is not found, then the object can at run-time either:
- Defer the call to another object in order to act as a proxy.
- Dynamically act on the selector.
- Dynamically bind an implementation to the previously unknown selector.
The most obvios advantage of a dynamically typed language with messages is what you see in delegates. As you might have noticed in for example the UITableViewDelegate
protocol; not all methods are declared as required.
This allows clients conforming to (implementing) a protocol to infer the default behavior simply by not implementing the delegate method at all. Whereas in for example Java it is quite common to apart from an interface Foo
also have an abstract default implementation FooAdapter
that implements default implementations for each and every method in the interface.
精彩评论