[swift-evolution] Remove AnyObject Constraint for Objective-C Lightweight Generics
phausler at apple.com
Thu Nov 30 13:14:17 CST 2017
> On Nov 29, 2017, at 2:07 PM, Riley Testut <rileytestut at gmail.com> wrote:
>> On Nov 9, 2017, at 9:01 AM, Philippe Hausler <phausler at apple.com <mailto: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.
Option 2 may take a lot of effort just as a heads up since that will mean that we would need to audit the entire macOS, iOS, tvOS, and watchOS SDKs and find any edge cases (my guess is very very few and perhaps only Foundation)
> 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.
NSMapTable or NSHashTable might be better examples; if the key or value is weakly stored the translated reference will drop off if the held structure is mutated.
var m = NSMapTable<NSString, NSData>(keyOptions: [.copyIn, .objectPersonality], valueOptions: [.weakMemory, .objectPersonality])
var key = "hello" as NSString
var value = Data(bytes: [0, 1, 2, 3]) as NSData
m.setObject(value, forKey: key)
assert(m.object(forKey: key) != nil)
That works as expected - so lets change it to a structure
var m = NSMapTable<String, Data>(keyOptions: [.copyIn, .objectPersonality], valueOptions: [.weakMemory, .objectPersonality])
var key = "hello"
var value = Data(bytes: [0, 1, 2, 3])
m.setObject(value, forKey: key) // after here there are no more references to value so it is destroyed
assert(m.object(forKey: key) != nil) // this now fails
In that second example even if you had a usage of value past the setObject method call that was a mutation it would also fail the assert because the backing reference would have changed.
I guess what I am saying is there are edge cases that we have to be careful with.
>> 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).
Dont get me wrong; I think this is a great idea and vastly improves the state of affairs – imho it is very well worth doing and getting this done before we cannot change it.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution