Last active
March 13, 2018 22:06
-
-
Save bdash/54014e09a79a6145ed26978b5bc23b96 to your computer and use it in GitHub Desktop.
Using Objective-C generics with type constraints on forward-declared classes?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
Compiling this file with
cc generics.m
gives: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 fromBase
. What's not clear is how to work around this without removing the generics. The circular dependency betweenDerived1
andDerived2
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?