views:

63

answers:

2
- (void) doSomething: (id)with {
    int a;
    a = [with doSomething];
}

How does the compiler know what type [with doSomething] is going to return? Does it assume an int, since that's what I'm assigning to?

+3  A: 

It doesn't know for sure what type is returned, because doSomething could be implemented with a different return type by several different classes.

I believe the compiler looks for all method implementations with that name, and makes sure at least one of them returns an int in the example above. If none of the "doSomething" signatures returned an int, it would give a warning.

Chris Garrett
+3  A: 

The compiler knows all the methods that are currently visible. If it has a type for the message that you're sending, it uses that type. If it knows of multiple method types that all have that selector, it will arbitrarily choose one, leading to warnings and probably bugs. If it doesn't know a type for any such method, it will assume that the method returns an id.

Chuck