# What are some practical use cases of AnyHashable?

**URL:** <https://forums.swift.org/t/what-are-some-practical-use-cases-of-anyhashable/51328>\
**Category:** Using Swift\
**Created:** [August 19, 2021, 3:46am UTC](https://forums.swift.org/t/what-are-some-practical-use-cases-of-anyhashable/51328 "2021-08-19T03:46:08Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [August 25, 2021, 10:40pm UTC](https://forums.swift.org/t/what-are-some-practical-use-cases-of-anyhashable/51328/8 "2021-08-25T22:40:28Z")

</div>

Also note that `AnyHashable` is quite magical compared to other `Any*` type erasers. You get implicit conversion to it:

```swift
let space: CoordinateSpace = .named(123) // no `as AnyHashable` needed

```

and you can cast back from `AnyHashable` to the concrete type:

```swift
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:

```swift
  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:

```swift
  1> let seq: AnySequence<Int> = .init([123])
seq: AnySequence<Int> = { ... }

```

And you cannot cast back to the concrete type:

```swift
  2> seq as? [Int]
$R0: [Int]? = nil

```

---

_[View the full topic](https://forums.swift.org/t/what-are-some-practical-use-cases-of-anyhashable/51328)._
