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

Philippe Hausler phausler at apple.com
Thu Nov 9 11:01:27 CST 2017


I have personally filed a few bugs on this; and I definitely consider it a bug that we cannot store Any in generics for objc. There are however some problem areas that might be worth considering while fixing this bug. 

1) We need to ensure this does not cause source churn - I would expect swift 4 to be source compatible with swift 5.

2) There are a few cases that might be a bit cagey - you claim NSCache, but would it be surprising that the boxed object having no refs gets purged? How bout NSPointerArray? 

3) Since Foundation is likely the most impact here I think it would be useful to audit the results of this before pushing it out; specifically the Foundation internal builds so that we can make sure the things we are working on function correctly.

Do you have implementations in the works yet? I really think this is important for us to get in (especially before the ABI gets locked down cause it could have impact there…)

> 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 <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 <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?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171109/8a196c3d/attachment.html>


More information about the swift-evolution mailing list