Currently (in Swift 4.2.1), it is an error to cast a value from Any
to Optional
, even when that value was created as the exact same Optional
type and previously cast to Any
. Here is an example:
let x: Int? = 1
let y: Any = x as Any
print(type(of: y)) // "Optional<Int>\n"
let z: Int? = y as! Int? // error: cannot downcast from 'Any' to a more optional type 'Int?'
Is this a bug, or an oversight, or an intentional design decision? In particular, would it require an evolution proposal to make it possible for this to work? My expectation was that Any
could wrap any type, and be unwrapped back to that type.
So far the only way I have found to extract the original Optional
value, is via a generic helper function such as:
func unwrap<T>(_ x: Any) -> T {
return x as! T
}
let z: Int? = unwrap(y)
print(type(of: z)) // "Optional<Int>\n"
It seems rather peculiar that we can attempt to unwrap an Any
into an unspecified generic type, but we are prohibited from unwrapping an Any
into the exact specific type we know it contains, if that type is Optional
.