Also note that AnyHashable
is quite magical compared to other Any*
type erasers. You get implicit conversion to it:
let space: CoordinateSpace = .named(123) // no `as AnyHashable` needed
and you can cast back from AnyHashable
to the concrete type:
let h: AnyHashable = 123
print(h as? Double) // prints nil
print(h as? Int) // prints Optional(123)
Compare to, for example, AnySequence
. There is no implicit conversion to it:
1> let seq: AnySequence<Int> = [123]
error: repl.swift:1:29: error: cannot convert value of type '[Int]' to specified type 'AnySequence<Int>'
let seq: AnySequence<Int> = [123]
^~~~~
You must manually construct it:
1> let seq: AnySequence<Int> = .init([123])
seq: AnySequence<Int> = { ... }
And you cannot cast back to the concrete type:
2> seq as? [Int]
$R0: [Int]? = nil