Another try:
private protocol _OptionalProtocol {
var _deepUnwrapped: Any? { get }
}
extension Optional: _OptionalProtocol {
fileprivate var _deepUnwrapped: Any? {
if let wrapped = self { return deepUnwrap(wrapped) }
return nil
}
}
func deepUnwrap(_ any: Any) -> Any? {
if let optional = any as? _OptionalProtocol {
return optional._deepUnwrapped
}
return any
}
deepUnwrap(1) // 1
deepUnwrap(Optional<Int>.none) // nil
deepUnwrap(Optional<Int>.some(1)) // 1
deepUnwrap(Optional<Optional<Int>>.none) // nil
deepUnwrap(Optional<Optional<Int>>.some(Optional<Int>.none)) // nil
deepUnwrap(Optional<Optional<Int>>.some(Optional<Int>.some(1))) // 1