I am surprised that “extension [Any?: Any?]? {}” compiles at all.
The compiler correctly prohibits declaring a variable of that type:
let x: [Any? : Any?]?
// error: 'Dictionary' requires that 'Any' conform to 'Hashable'
It is not clear to me what exactly you are trying to do. What type or types are you trying to extend? What type do you expect “Key” to represent?
Are you trying to create a parameterized extension of Optional where Wrapped is a Dictionary with unspecified generics?
…or where Wrapped is a Dictionary for which both Key and Value are themselves Optional types with unspecified Wrapped values? That seems inadvisable even if it were to become possible.
The type of dict in your example is Dictionary?, which is the same as Optional, so the type you need to extend here is Optional (where Wrapped == Dictionary), not Dictionary itself.
Thanks for your reply!
Could you please give me a working example? I have tried this, but there are still several errors:
extension Optional where Wrapped == Dictionary<Any?, Any?> { // 'Dictionary' requires that 'Any' conform to 'Hashable', Type 'Any' does not conform to protocol 'Hashable'
func containsKey(_ key: Key) -> Bool { // Cannot find type 'Key' in scope
guard let self else { return false }
return keys.contains(key) // Cannot find 'keys' in scope
}
}