We have just updated Xcode to 10.2 (hence iOS 12.2 SDK) and started seeing odd behavior with respect to the behavior of Swift Generics
and Optionals
. We have kept the Swift version at 4.2, so no Swift 5 update. The only change was updating to Xcode 10.2 from Xcode 10.1.
Here is a sample code that illustrates the oddities. The comments show what has changed between versions. Ideally, there shouldn't be any changes.
class Phone<T> {}
extension Phone {
class func create(initial: T? = nil) -> Phone<T> {
if let _ = initial { print("Regular: Unwrapping worked.") }
return Phone()
}
}
extension Phone where T == Void {
class func create(initial: T? = nil) -> Phone<T> {
if let _ = initial { print("T == Void: Unwrapping worked.") }
return Phone()
}
}
var phone: Phone<Int?> = Phone()
var phone2: Phone<Int?> = Phone()
var phone3: Phone<Int?> = Phone()
// unwrapping works iOS 12.1, doesn't work in 12.2
phone = Phone.create(initial: Optional(nil))
// unwrapping works iOS 12.1, doesn't work in 12.2
phone2 = Phone.create(initial: Optional<Int?>(nil))
// doesn't compile in iOS 12.1, unwrapping works in iOS 12.2
phone3 = Phone.create(initial: Optional<Int>(nil))
// doesn't compile in iOS 12.1, unwrapping doesn't work in 12.2 (uses the T == Void function)
let phone4 = Phone.create(initial: Optional(nil))
We have gone through the release notes of Xcode 10.2 but haven't spotted any changes around Optionals or Generics. It is really hard to understand what is causing this change in behavior between versions.
It is especially very interesting how phone2
and phone3
behave differently. There are few odd things happening in the code sample above, so the question is if anyone knows what might have caused the behavioral changes in this release?