[swift-evolution] Pitch: Deprecate/remove AnyObject method dispatch
Jon Shier
jon at jonshier.com
Tue Oct 24 17:09:30 CDT 2017
Slava:
How would this change affect this code from Alamofire, used to compare two SecKey instances for public key pinning:
outerLoop: for serverPublicKey in ServerTrustPolicy.publicKeys(for: serverTrust) as [AnyObject] {
for pinnedPublicKey in pinnedPublicKeys as [AnyObject] {
if serverPublicKey.isEqual(pinnedPublicKey) {
serverTrustIsValid = true
break outerLoop
}
}
}
I’m not sure why, but the AnyObject casting makes this work correctly, even in Swift 4. I’ve tried to “modernize” it by using Sets (compare the pinned keys with the available keys Sets) but it doesn’t work (hash implementation for SecKey is weird?). I’m guessing that with the AnyObject case and isEqual, some underlying implementation is changed and equality works properly (or well enough for this use). I’m happy to get rid of this hack, but I’m not sure of any other method to accomplish our goals here.
Jon Shier
> On Oct 24, 2017, at 6:02 PM, Slava Pestov via swift-evolution <swift-evolution at swift.org> wrote:
>
> Hi all,
>
> Dynamic dispatch of methods through AnyObject is a source of implementation complexity, has many known bugs which we are not going to fix, and no longer makes sense now in an id-as-Any world; AnyObject is not the ‘common currency’ type for arbitrary Objective-C objects anymore.
>
> I would like to propose we deprecate it as follows:
>
> - Unconditional warning in Swift 4.1, with a fixit to add an ‘as’ cast to cast the base value to the right type
> - Error in Swift 5 in -swift-version 5 mode
>
> Thoughts? Does anyone actually rely on this feature, instead of just stumbling on it by accident once in a while?
>
> Slava
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list