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

Riley Testut rileytestut at gmail.com
Wed Nov 8 13:49:47 CST 2017


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171108/8a8b337a/attachment.html>


More information about the swift-evolution mailing list