Back when SE-0057
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
was accepted, which allowed for bridging any type to Objective-C, not just
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.
swift-evolution mailing list