Objective-C generics not visible from Swift?

It seems that when I create a generic class in Objective-C, like this:


#import <Foundation/Foundation.h>

#if __has_feature(objc_generics)
@interface CSEither<LeftType, RightType> : NSObject

- (nonnull instancetype)initWithLeftValue:(nonnull LeftType)left;
- (nonnull instancetype)initWithRightValue:(nonnull RightType)right;

@property (nullable, nonatomic, readonly, strong) LeftType left;
@property (nullable, nonatomic, readonly, strong) RightType right;

@end
#endif


the generated Swift interface ends up looking like this:


import Foundation
class CSEither : NSObject {
  
    init(leftValue left: AnyObject)
    init(rightValue right: AnyObject)
}


i.e. all the generic type information is stripped out, as are all properties that use them.


Is this intended behavior, or am I doing something wrong?


Thanks!

You aren't doing anything wrong, the Swift compiler simply doesn't import Obj-C lightweight generics. Feel free to Report a Bug asking for this behavior.

That's shocking, since Swift<->ObjC interoperability would seem to be the major reason for the introduction of ObjC lightweight generics. Is this really not intended to cross over to Swift, or has it just not been implemented yet?

I think the point is that Obj-C generics were intended to be consumed by the bridging process, not propagated. Here is what the Swift/Cocoa book says:


Lightweight Generics


Objective-C declarations of NSArray, NSSet and NSDictionary types using lightweight generic parameterization are imported by Swift with information about the type of their contents preserved.

NOTE


Aside from than these Foundation collection classes, Objective-C lightweight generics are ignored by Swift. Any other types using lightweight generics are imported into Swift as if they were unparameterized.

Objective-C generics not visible from Swift?
 
 
Q