[LLVMbugs] [Bug 10133] New: Objective-C should adopt `auto` keyword from C++0x

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Jun 13 14:33:41 PDT 2011


http://llvm.org/bugs/show_bug.cgi?id=10133

           Summary: Objective-C should adopt `auto` keyword from C++0x
           Product: clang
           Version: unspecified
          Platform: Macintosh
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: kyle at omnigroup.com
                CC: llvmbugs at cs.uiuc.edu


C++0x overloads the auto keyword to support type inference in declarations.
This makes declarations much shorter and more readable without sacrificing
static type safety. Objective-C should adopt this feature, with the additional
enhancement of using `auto` in the return type of a declaration to refer to the
type of the receiver (or an instance of the receiver in class methods). For
example:

@interface MyObject : NSObject

+ (auto)sharedInstance; // every subclass has its own shared instance
+ (NSString *)defaultName;

- (auto)initWithName:(NSString *)name;

@property(readonly, nonatomic) NSString *name;

@end

@implementation MyObject

+ (auto)sharedInstance;
{
    static NSDictionary classToInstanceMap;
    if (!classToInstanceMap)
      classToInstanceMap = [[NSDictionary alloc] init];

    NSString *className = NSStringFromClass(self);
    MyObject *instance = [classToInstanceMap valueForKey:className];
    if (!instance) {
      instance = [[[self alloc] initWithName:[self defaultName]] autorelease];
      [classToInstanceMap setValue:instance forKey:className];
    }

    return instance;
}

+ (NSString *)defaultName;
{
    return @"defaultInstance";
}

- (auto)initWithName:(NSString *)name;
{
    return [[self class] initWithName:name];
}

@end

// ---

@interface SpecialObject : MyObject

@property(readonly, nonatomic) NSUInteger numberOfFoos;

@end

// ---

void DoSomething()
{
    auto anObject = [SpecialObject sharedInstance];
    NSLog(@"%lu", anObject.numberOfFoos); // okay; type inference says that
+[SpecialController sharedController] returns an instance of SharedController

    auto nFoos = anObject.numberOfFoos;
    NSLog(@"%@", nFoos.intValue); // error: invalid type `unsigned long` of
receiver

    anObject = [[MyObject alloc] init]; // error: anObject was already
determined to be of type MyObject at declaration

    auto anotherObject; // error: can't infer type without initializer
}

// ----****----

It's worth noting the compiler still needs the hardcoded knowledge that
-[NSObject class] returns an instance of the _metaclass_ of the receiver.
Alternatively, a new keyword like `auto Class` could be used here that is only
valid in the return type of a method declaration.

These keywords should all be implemented in terms of implied properties. That
is to say, these two declarations should be equivalent:

- (auto)initWithFoo:(id)foo;
- (id)initWithFoo:(id)foo __attribute__((__auto_return__));

As should these:

- (auto Class)class;
- (Class)class __attribute__((__auto_class_return__));

The programmer should be instructed to keep using explicit type declarations
where it makes sense--that is, where the intended API guarantee is only that
instances of a certain type are returned, even if a subclass overrides the
method. For example, +[NSDocumentController sharedDocumentController] should
*not* be declared with `auto`, because calling +sharedDocumentController on any
subclass should always return the same value, even if that instance is not of
the type of an instance of the receiver.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list