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

Riley Testut rileytestut at gmail.com
Wed Nov 29 16:07:59 CST 2017


> On Nov 9, 2017, at 9:01 AM, Philippe Hausler <phausler at apple.com> wrote:
> 
> 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.

Agreed. I'd be surprised if this would cause churn though, since this is effectively just loosening a restriction, and all existing use cases would still be allowed.

> 
> 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? 

I agree there are certain cases where true reference semantics are important, and off the top of my head I can think of two (relatively easy) ways we could accommodate this:

1) The Objective-C class declaration explicitly specifies an upper-bound of NSObject (or NSObjectProtocol).
2) Add a new keyword (similar to existing __covariant and __contravariant keywords) such as __reference (where the final name would of course be bike-shedded)

I’m leaning towards an approach similar to 2) since 1) might be confusing to newcomers due to it seemingly have no purpose considering the NSObject constraint would implicitly there where using the generic class from Objective-C code.

I’m not familiar with NSCache’s internals, so I wasn’t aware references play a role in whether or not NSCache purges an object. That being said, I don’t think it would be surprising if NSCache purged a large Data value under memory pressure, as long as it didn’t affect any “copies” I had retrieved and was currently using. 

As for NSPointerArray, we’d still need to get Objective-C generics for it first 😉 Though assuming that is added, the generic parameter would need to explicitly say it requires a reference.

> 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…)


No I don’t, but would be open to digging into it and seeing what I could do as a proof-of-concept (I just don’t know where I’d start looking to accomplish this).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171129/05e10393/attachment.html>


More information about the swift-evolution mailing list