On Jul 10, 2008, at 8:55 AM, an0 wrote:
Sure.
I'm grateful that you tell me the internal truth instead of confusing
me even more by just saying it is my responsibility to tell compiler
more.
But if different return types cause different native code, how could
my program still work with the mistaken type(an NSString * returned
from the inner message is treated as an NSInteger at the first place,
then is passed as an NSString * to the outer message) guessed by
compiler?

Whichever method the compiler sees first wins.

While Objective-C is polymorphic, it does not support type based dispatch or type based method differentiation (like, say, Java).

In Objective-C, the following doesn't make sense:

- (NSInteger) tag;
- (NSString *) tag;

It won't compile if in the same class file and, as you have discovered, it'll cause no end of problems when the same method name -- the same selector -- has different argumentation across different classes in the class hierarchy.

To put it more precisely: Objective-C has a single, global, namespace for all methods. Every method's name, every selector, is in a shared namespace. The method's selector does not include any typing information and, thus, the type of the arguments and return value of the method are not used by the compiler to disambiguate invocations.

As a result, the standard pattern is to *never* declare the same method name twice, but with different types of arguments or return values.

For someone coming from C++ or Java, this may seem like a pretty nasty restriction. It really isn't. It is just different. And it has some very distinct advantages. Two, in fact:

- there is no name mangling

- you don't have to figure out the types of the arguments to figure out which of N possible methods on your class, all named identically save for argumentation differences, were invoked

I.e. it is dead simple.

b.bum

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to