Skip to content

Instantly share code, notes, and snippets.

@bdash
Last active March 13, 2018 22:06
Show Gist options
  • Save bdash/54014e09a79a6145ed26978b5bc23b96 to your computer and use it in GitHub Desktop.
Save bdash/54014e09a79a6145ed26978b5bc23b96 to your computer and use it in GitHub Desktop.
Using Objective-C generics with type constraints on forward-declared classes?
#import <Foundation/Foundation.h>
@interface Base : NSObject
@end
@interface GenericCollection<T: Base *> : NSObject
@end
@class Derived2;
@interface Derived1 : Base
@property (strong) GenericCollection<Derived2 *> *collection;
@end
@interface Derived2 : Base
@property (strong) GenericCollection<Derived1 *> *collection;
@end
@bdash
Copy link
Author

bdash commented May 1, 2016

Compiling this file with cc generics.m gives:

generics.m:12:38: error: type argument 'Derived2 *' does not satisfy the bound ('Base *') of type parameter 'T'
@property (strong) GenericCollection<Derived2 *> *collection;
                                     ^
generics.m:6:30: note: type parameter 'T' declared here
@interface GenericCollection<T: Base *> : NSObject
                             ^
1 error generated.

It's clear why the error occurs: the compiler has only seen a forward-declaration of Derived2 and so does not know that it satisfies the constraint of deriving from Base. What's not clear is how to work around this without removing the generics. The circular dependency between Derived1 and Derived2 necessitates a forward declaration of at least one of those classes, which seems to prohibit ever using that class as the type argument in a constrained generic. Is removing the type constraint (or the use of generics) the only solution?

@mitzpettel
Copy link

mitzpettel commented May 1, 2016

Deferring the property declaration to a class extension works:

#import <Foundation/Foundation.h>

@interface Base : NSObject
@end

@interface GenericCollection<T: Base *> : NSObject
@end

@interface Derived1 : Base
@end

@interface Derived2 : Base
@property (strong) GenericCollection<Derived1 *> *collection;
@end

@interface Derived1 ()
@property (strong) GenericCollection<Derived2 *> *collection;
@end

In a real-world setting it would likely require introducing an additional header file that both Derived1.h and Derived2.h import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment