let f: () -> Void = { }
let array = ["a", 1, false, f] as [Any]
if array[3] is AnyObject {
print(array[3])
}
Why is the element evaluating to true for AnyObject even though the array is set to store Any?
Why is function evaluates to true as an AnyObject even though AnyObject by definition can only be classes?
As another example:
let f: () -> Bool = { return true }
let ff = f as AnyObject
(ff as () -> Bool)()
This goes against the definition of AnyObject in the API doc, which states that:
AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol.
or the official Swift Programming Language Guide:
• Any can represent an instance of any type at all, including function types.
• AnyObject can represent an instance of any class type.
In the above example, it looks like function can be representated as AnyObject.
There is explanation elsewhere in SO (as pointed out by @hamish) that internally because SwiftValue class is used, anything can be bridged to AnyObject. The logic of explanation seems to be flawed/reverse, as we should make implementation conform to the language definition, not the other way around, so either the implementation is incorrect, or the definition of AnyObject and typecheck operator is incorrect?