[swift-evolution] Remove AnyObject Constraint for Objective-C Lightweight Generics

Joe Groff jgroff at apple.com
Wed Nov 8 13:51:27 CST 2017



> On Nov 8, 2017, at 11:49 AM, Riley Testut via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi Swift-Evolution,
> 
> Back when SE-0057 (https://github.com/apple/swift-evolution/blob/master/proposals/0057-importing-objc-generics.md) was proposed, it included the following passage:
> 
> 	• The generic type parameters in Swift will always be class-bound, i.e., the generic class will have the requirement T : AnyObject.
> This made sense at the time, since Swift <-> Objective-C interoperability was only possible with class types (AnyObject). However, several months after SE-0057 was accepted, SE-0116 (https://github.com/apple/swift-evolution/blob/master/proposals/0116-id-as-any.md) was accepted, which allowed for bridging any type to Objective-C, not just class types.
> 
> This greatly improved interoperability between Swift and Objective-C code, but the AnyObject restriction on Objective-C generics remained. This issue is especially apparent when using lesser-known Objective-C collection types such as NSCache, where it may make sense to store value types or use value types as the keys, but the compiler does not allow it.
> 
> I propose that this restriction is lifted, and that generic Objective-C parameters are no longer restricted to conforming to AnyObject. I’m assuming this is not as straightforward as it might seem at first to implement, but I think the benefits would make the effort worth it, since this seems like an overlooked case and not intentionally kept this way.
> 
> Thoughts?

In principle it makes sense, but there are implementation challenges we didn't have time to consider. It would be nice to make this happen when we have the time to make it work.

-Joe



More information about the swift-evolution mailing list