LLVM Enhancement: @switch

Originator:oliver.drobnik
Number:rdar://14557382 Date Originated:Jul 26 2013
Status:Open Resolved:
Product:LLVM Product Version:
Classification:Enhancement Reproducible:N/A
 
Summary:

The classic C statement is useless for use in Objective-C if you want to use NSString literals as constants for the cases. This enhancement request is for adding an object-oriented version of the switch to Objective-C.

For example this could look like this:


   define MyBarConstantValue @"bar"

    NSString *string = @"foo";
   @switch (string) {
      @"foo": 
         // statements
        break;

      MyBarConstantValue:    
         // statements
         break;

      default:
         break;
   }


While a classic switch would do an == comparison between the expression and the labels this enhanced version would instead use isEqual: for the comparison. Otherwise the syntax would be identical.

Using this enhancement you could use any NSObject subclass which implements isEqual:. This would eliminate the need for creating enums for situations where an option is indeed an NSString. The above example currently has to be written:

   if ([string isEqual:@"foo"]) {
      // statements
   } else if ([string isEqual:MyBarConstantValue]) {
      // statements
   } else {
      // statements
   }

This is harder to parse than using the proposed @switch statement. Also this doesn't allow for a "fall-through" for a few options by omitting the break statement. So this would also have to be smart to resolve this scenario.

Comments

Remember that NSString has its own special method, -isEqualToString:


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!