Closed
Description
Previous ID | SR-14682 |
Radar | None |
Original Reporter | @jtbandes |
Type | Bug |
Status | Resolved |
Resolution | Done |
Additional Detail from JIRA
Votes | 0 |
Component/s | Compiler |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 56445a204e4486376f23f3428c35a2ec
Issue Description:
I ran into difficulties detecting Any?.some(Any?.none)
, because of confusion with availability of ==
operators, and surprising behavior of as?
casts (On the operators side, x == nil
is an allowed comparison with undesirable results, but x == (nil as Any?)
is not allowed.)
One concrete issue seems to be that a conditional as?
cast from Any to Any? produces a warning, but changing as?
to as
changes the final result, and removing the cast results in an error.
Perhaps my code below is still wrong, but it's hard to tell!
import Foundation
func wrap(_ x: Any) -> Any? {
return x
}
// Correctly detects Any?.none, but not Any?.some(Any?.none)
func isNil1(_ x: Any?) -> Bool {
if case .none = x {
return true
}
return false
}
// Correctly detects Any?.some(Any?.none), but compiler produces a warning
func isNil2(_ x: Any?) -> Bool {
// warning: Conditional cast from 'Any' to 'Any?' always succeeds
if let x = x, case .some(nil) = x as? Any? {
return true
}
// Note that `if let x = x, case .some(nil) = x { ... }` produces
// error: type of expression is ambiguous without more context
return false
}
// Correctly detects Any?.none, but not Any?.some(Any?.none)
func isNil3(_ x: Any?) -> Bool {
if let x = x, case .some(nil) = x as Any? {
return true
}
return false
}
isNil1(wrap(nil as Any?)) // false
isNil2(wrap(nil as Any?)) // true (desired)
isNil3(wrap(nil as Any?)) // false