Why must the last part of an Objective-C method name take an argument (when there is more than one part)? Why must the last part of an Objective-C method name take an argument (when there is more than one part)? objective-c objective-c

Why must the last part of an Objective-C method name take an argument (when there is more than one part)?


This is Brad Cox. My original answer misunderstood the question. I assumed reallyFast was a hardcoded extension to trigger faster messaging, not a kind of syntactic sugar. The real answer is that Smalltalk didn't support it, perhaps because its parser couldn't deal with the (assumed) ambiguity. Although OC's square brackets would remove any ambiguity, I simply didn't think of departing from Smalltalk's keyword structure.


21 years of programming Objective-C and this question has never crossed my mind. Given the language design, the compiler is right and the runtime functions are wrong ().

The notion of interleaved arguments with method names has always meant that, if there is at least one argument, the last argument is always the last part of the method invocation syntax.

Without thinking it through terribly much, I'd bet there are some syntactic bugaboos with not enforcing the current pattern. At the least, it would make the compiler harder to write in that any syntax which has optional elements interleaved with expressions is always harder to parse. There might even be an edge case that flat out prevents it. Certainly, Obj-C++ would make it more challenging, but that wasn't integrated with the language until years after the base syntax was already set in stone.

As far as why Objective-C was designed this way, I'd suspect the answer is that the original designers of the language just didn't consider allowing the interleaved syntax to go beyond that last argument.

That is a best guess. I'll ask one of 'em and update my answer when I find out more.


I asked Brad Cox about this and he was very generous in responding in detail (Thanks, Brad!!):

I was focused at that time on duplicating as much of Smalltalk as possible in C and doing that as efficiently as possible. Any spare cycles went into making ordinary messaging fast. There was no thought of a specialized messaging option ("reallyFast?" [bbum: I asked using 'doSomething:withSomething:reallyFast' as the example]) since ordinary messages were already as fast as they could be. This involved hand-tuning the assembler output of the C proto-messager, which was such a portability nightmare that some if not all of that was later taken out. I do recall the hand-hacked messager was very fast; about the cost of two function calls; one to get into the messager logic and the rest for doing method lookups once there.
Static typing enhancements were later added on top of Smalltalk's pure dynamic typing by Steve Naroff and others. I had only limited involvement in that.

Go read Brad's answer!


Just for your information, the runtime doesn't actually care about the selectors, any C string is valid, you could as well make a selector like that: "==+===+---__--¨¨¨¨¨^::::::" with no argument the runtime will accept it, the compiler just can't or else it's impossible to parse. There are absolutely no sanity check when it comes to selectors.