Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Objective-C, you can't declare method names where the last component doesn't take an argument. For example, the following is illegal.

-(void)take:(id)theMoney andRun; -(void)take:(id)yourMedicine andDontComplain; 

Why was Objective-C designed this way? Was it just an artifact of Smalltalk that no one saw a need to be rid of?

This limitation makes sense in Smalltalk, since Smalltalk doesn't have delimiters around message invocation, so the final component would be interpreted as a unary message to the last argument. For example, BillyAndBobby take:'$100' andRun would be parsed as BillyAndBobby take:('$100' andRun). This doesn't matter in Objective-C where square brackets are required.

Supporting parameterless selector components wouldn't gain us much in all the usual ways a language is measured, as the method name a programmer picks (e.g. runWith: rather than take:andRun) doesn't affect the functional semantics of a program, nor the expressiveness of the language. Indeed, a program with parameterless components is alpha equivalent to one without. I'm thus not interested in answers that state such a feature isn't necessary (unless that was the stated reasons of the Objective-C designers; does anyone happen to know Brad Cox or Tom Love? Are they here?) or that say how to write method names so the feature isn't needed. The primary benefit is readability and writability (which is like readability, only... you know), as it would mean you could write method names that even more closely resemble natural language sentences. The likes of -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication (which Matt Gallagher points out on "Cocoa With Love" is a little bit confusing when you drop the formal parameter) could be named -(BOOL)application:(NSApplication*)theApplication shouldTerminateAfterLastWindowClosed, thus placing the parameter immediately next to the appropriate noun.

Apple's Objective-C runtime (for example) is perfectly capable of handling these kind of selectors, so why not the compiler? Why not support them in method names as well?

#import <Foundation/Foundation.h> #import <objc/runtime.h>  @interface Potrzebie : NSObject -(void)take:(id)thing; @end  @implementation Potrzebie +(void)initialize {     SEL take_andRun = NSSelectorFromString(@"take:andRun");     IMP take_ = class_getMethodImplementation(self, @selector(take:));     if (take_) {         if (NO == class_addMethod(self, take_andRun, take_, "@@:@")) {             NSLog(@"Couldn't add selector '%@' to class %s.",                    NSStringFromSelector(take_andRun),                    class_getName(self));         }     } else {         NSLog(@"Couldn't find method 'take:'.");     } }  -(void)take:(id)thing {     NSLog(@"-take: (actually %@) %@",NSStringFromSelector(_cmd), thing); } @end  int main() {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];      Potrzebie *axolotl=[[Potrzebie alloc] init];     [axolotl take:@"paradichloroaminobenzaldehyde"];     [axolotl performSelector:NSSelectorFromString(@"take:andRun")                    withObject:@"$100"];     [axolotl release];      [pool release];     return 0; } 
like image 410
outis Avatar asked Dec 18 '10 20:12

outis


1 Answers

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.

like image 95
Brad Cox Avatar answered Oct 19 '22 07:10

Brad Cox