objective-cgenericscovariant

Objective-C Generics covariant


Hiyas

I have this interface:

typedef void (^RMIteratorCompletionBlock) (void);

@interface RMAsyncIterator<__covariant T> : NSObject

+(RMAsyncIterator<T>*) iteratorWithArray:(NSArray<T>*) array;

-(id) init NS_UNAVAILABLE;
-(id) initWithArray:(NSArray*) array NS_DESIGNATED_INITIALIZER;

-(void) iterateWithWorkerBlock:(void (^) (T object, RMAsyncIterator<T>* iterator)) block withCompletionBlock:(RMIteratorCompletionBlock) completionBlock;
-(void) advance;
-(void) complete;

@end

now, when I use this implementation method:

-(void) iterateWithWorkerBlock:(void (^) (id object, RMAsyncIterator* iterator)) block withCompletionBlock:(RMIteratorCompletionBlock) completionBlock {

I rightfully get a compiler warning about Conflicting parameter types in implementation of iterateWithWorkerBlock:withCompletionBlock:

The thing is.. how the hell are you supposed to write this implementation method? I tried a variety of different implementations, moving the type out into a typedef failed because the type is not visible outside of the interface scope, using T as type in the implementation fails because apparently, the implementation knows nada about its own interface type, using something like

-(void) iterateWithWorkerBlock:(void (^) (<__covariant T> object, RMAsyncIterator* iterator)) block withCompletionBlock:(RMIteratorCompletionBlock) completionBlock {

failed miserably and I really don't know how to implement this method correctly. For now, I simply use id, which gives me this warning, but I'd like to get rid of the warning...

Bueller? Anyone? Thanks


Solution

  • Just use id type

    -(void) iterateWithWorkerBlock:(void (^) (id object, RMAsyncIterator* iterator)) block withCompletionBlock:(RMIteratorCompletionBlock) completionBlock {