Even though AnyHashable
is type erased, it's still type specific you must know its type in order to cast it back to its original type, right?
How does AnyHashable
get this power? NSNumber
also can be casted this way to any numeric types and Bool
. Compiler special bestow magic to these types? Are there more of these magical types?
Without knowing AnyHashable
's special cast ability, I was doing this:
anAnyHashable.base as? Int
now I know it's not necessary to use .base
, just cast directly. Even the the doc on .base is show this.. The doc should make clear cast directly on AnyHashable
is allowed.
dump(anAnyHashable)
▿ AnyHashable(6)
- value: 6
AnyHashable
has this value
field, and the type is the actual type. So AnyHashable
is very special, like a generic.
The standard library type AnyHashable is written partially in C++