# \#hashable

**URL:** https://forums.swift.org/tag/hashable/117.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [\[Pitch\] Add RawSpan primitive to Hasher](https://forums.swift.org/t/pitch-add-rawspan-primitive-to-hasher/89583)

<div class="topic-metadata">

**Author:** [@jmschonfeld](https://forums.swift.org/u/jmschonfeld)\
**Replies:** 3\
**Last updated:** [October 12, 2026, 11:12pm UTC](https://forums.swift.org/t/pitch-add-rawspan-primitive-to-hasher/89583 "2026-10-12T23:12:26Z")

</div>

Hi everyone, I have a short pitch to add a RawSpan primitive to Hasher to mirror the existing unsafe buffer pointer API. The swift-evolution PR is up at Add RawSpan primitive to Hasher by jmschonfeld · Pull Request #3490…

---

## [\[Pitch\] Default implementation of Hashable for AnyObject](https://forums.swift.org/t/pitch-default-implementation-of-hashable-for-anyobject/88853)

<div class="topic-metadata">

**Author:** [@DominikGrodl](https://forums.swift.org/u/DominikGrodl)\
**Replies:** 51\
**Last updated:** [August 15, 2026, 1:00pm UTC](https://forums.swift.org/t/pitch-default-implementation-of-hashable-for-anyobject/88853 "2026-08-15T13:00:13Z")

</div>

Hello friends, I'd like to pitch a very small, but IMHO a very useful change when it comes to conforming AnyObjects to Hashable. Motivation Nowadays it is common to conform classes to Hashable when building SwiftUI app…

---

## [\[Pitch\] Automatic \`Hashable\` Conformance for Tuples (Revival of SE-0283)](https://forums.swift.org/t/pitch-automatic-hashable-conformance-for-tuples-revival-of-se-0283/83105)

<div class="topic-metadata">

**Author:** [@joshuacleetus](https://forums.swift.org/u/joshuacleetus)\
**Replies:** 13\
**Last updated:** [March 16, 2026, 12:38am UTC](https://forums.swift.org/t/pitch-automatic-hashable-conformance-for-tuples-revival-of-se-0283/83105 "2026-03-16T00:38:36Z")

</div>

\[Pitch\] Automatic \`Hashable\` Conformance for Tuples (Revival of SE-0283) UPDATE (Nov 2025): Incorporated feedback from @Slava\_Pestov and @allevato – removed invalid syntax, clarified \`Array\` hashing, removed non-existen…

---

## [Why does casting AnyHashable to NSObject always succeed?](https://forums.swift.org/t/why-does-casting-anyhashable-to-nsobject-always-succeed/82517)

<div class="topic-metadata">

**Author:** [@a7medev](https://forums.swift.org/u/a7medev)\
**Replies:** 2\
**Last updated:** [October 5, 2025, 10:30pm UTC](https://forums.swift.org/t/why-does-casting-anyhashable-to-nsobject-always-succeed/82517 "2025-10-05T22:30:30Z")

</div>

While experimenting with AnyHashable, I was surprised that the following code actually works. import Foundation struct S: Hashable { var a: Int } let hashable = AnyHashable(S(a: 5)) let object: NSObject = hashable…

---

## [How to implement the same Hashability for different values?](https://forums.swift.org/t/how-to-implement-the-same-hashability-for-different-values/78974)

<div class="topic-metadata">

**Author:** [@ylorn](https://forums.swift.org/u/ylorn)\
**Replies:** 3\
**Last updated:** [March 31, 2025, 9:42pm UTC](https://forums.swift.org/t/how-to-implement-the-same-hashability-for-different-values/78974 "2025-03-31T21:42:08Z")

</div>

Let's say we have a type Foo that can be created locally or delivered from cloud. struct Foo: Hashable, Sendable { public enum ID: Hashable, Sendable { case local(String) case cloud(String) c…

---

## [Custom AnyXYZ with implicit casting (like AnyHashable)](https://forums.swift.org/t/custom-anyxyz-with-implicit-casting-like-anyhashable/74635)

<div class="topic-metadata">

**Author:** [@pnewell](https://forums.swift.org/u/pnewell)\
**Replies:** 8\
**Last updated:** [September 16, 2024, 9:19pm UTC](https://forums.swift.org/t/custom-anyxyz-with-implicit-casting-like-anyhashable/74635 "2024-09-16T21:19:29Z")

</div>

Has anyone explored implementing a custom AnyXYZ wrapper that works like AnyHashable? For example, if I want to have a AnyHashableCodable wrapper that has similar syntactical sugar so I could do the following: var dict…

---

## [Hash(into:) vs hashValue](https://forums.swift.org/t/hash-into-vs-hashvalue/71037)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 1\
**Last updated:** [April 2, 2024, 6:09pm UTC](https://forums.swift.org/t/hash-into-vs-hashvalue/71037 "2024-04-02T18:09:00Z")

</div>

How is this magic done? Could I achieve the same 1...4 behaviour with my foo & bar methods of my P protocol, or is this special trick baked into the compiler? I can implement just hash(into:), in this case calling has…

---

## [Role of \`Equatable\` conformance when inserting element inside a \`Set\`](https://forums.swift.org/t/role-of-equatable-conformance-when-inserting-element-inside-a-set/69449)

<div class="topic-metadata">

**Author:** [@NicFontana](https://forums.swift.org/u/NicFontana)\
**Replies:** 6\
**Last updated:** [January 15, 2024, 4:18pm UTC](https://forums.swift.org/t/role-of-equatable-conformance-when-inserting-element-inside-a-set/69449 "2024-01-15T16:18:15Z")

</div>

Hi all, I am playing around with this sample code inside a Playground: import Foundation struct Operation: Hashable { let subject: String let type: OperationType init(subject: String, type: OperationT…

---

## [Warning about \`hasher.combine(self)\`](https://forums.swift.org/t/warning-about-hasher-combine-self/64769)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 8\
**Last updated:** [May 3, 2023, 5:06am UTC](https://forums.swift.org/t/warning-about-hasher-combine-self/64769 "2023-05-03T05:06:05Z")

</div>

Can we have a warning here? func hash(into hasher: inout Hasher) { hasher.combine(self) // wanted warning: Function call causes an infinite recursion }

---

## [Calling though \`super\` ==](https://forums.swift.org/t/calling-though-super/61519)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 1\
**Last updated:** [November 18, 2022, 3:52pm UTC](https://forums.swift.org/t/calling-though-super/61519 "2022-11-18T15:52:47Z")

</div>

There seems to be no way to call the autosynwthesized EQ / hash implementation from within a custom implementation: static func == (lhs: Self, rhs: Self) -\> Bool { // pseudo code: var result = "original authosyn…

---

## [Why does Hashable require Equatable?](https://forums.swift.org/t/why-does-hashable-require-equatable/16817)

<div class="topic-metadata">

**Author:** [@nh7a](https://forums.swift.org/u/nh7a)\
**Replies:** 57\
**Last updated:** [November 8, 2022, 8:37pm UTC](https://forums.swift.org/t/why-does-hashable-require-equatable/16817 "2022-11-08T20:37:57Z")

</div>

Hi there, Recently, I've experienced an obscure bug using Dictionary caused by mismtatching implementation of hashValue and ==, something like below. struct Foo { let n, m: Int } extension Foo: Equatable { static fun…

---

## [Possible Bug in Swift 5.6 stdlib: Custom Hashable implementation in protocol extension causes infinite loop when used by RawRepresentable types](https://forums.swift.org/t/possible-bug-in-swift-5-6-stdlib-custom-hashable-implementation-in-protocol-extension-causes-infinite-loop-when-used-by-rawrepresentable-types/58536)

<div class="topic-metadata">

**Author:** [@retendo](https://forums.swift.org/u/retendo)\
**Replies:** 5\
**Last updated:** [June 30, 2022, 12:22am UTC](https://forums.swift.org/t/possible-bug-in-swift-5-6-stdlib-custom-hashable-implementation-in-protocol-extension-causes-infinite-loop-when-used-by-rawrepresentable-types/58536 "2022-06-30T00:22:06Z")

</div>

Steps to reproduce: Create a new interface builder based iOS app Navigate to ViewController.swift Paste the following code import UIKit protocol P: Hashable { var name: String { get } } extension P { func has…

---

## [Substring violates Hashable's requirements - stdlib/compiler bug?](https://forums.swift.org/t/substring-violates-hashables-requirements-stdlib-compiler-bug/58046)

<div class="topic-metadata">

**Author:** [@Jens](https://forums.swift.org/u/Jens)\
**Replies:** 6\
**Last updated:** [June 11, 2022, 11:52pm UTC](https://forums.swift.org/t/substring-violates-hashables-requirements-stdlib-compiler-bug/58046 "2022-06-11T23:52:06Z")

</div>

I bumped into what I guess might be a bug in the standard library or compiler. The actual code and situation where it happened is quite involved and this is the smallest program I could make that reproduces the bug: fu…

---

## [On purity grades of value types \[was: ValueType protocol\]](https://forums.swift.org/t/on-purity-grades-of-value-types-was-valuetype-protocol/52139)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 31\
**Last updated:** [September 21, 2021, 2:55pm UTC](https://forums.swift.org/t/on-purity-grades-of-value-types-was-valuetype-protocol/52139 "2021-09-21T14:55:36Z")

</div>

update it turns out the example i provided was not correct, thanks to @Nickolas\_Pohilets for pointing it out. the new corrected example provided basically it is an example that: invalidates Set/Dictionary invariants (…

---

## [Set Uniqueness of instances with the same hashValue](https://forums.swift.org/t/set-uniqueness-of-instances-with-the-same-hashvalue/45235)

<div class="topic-metadata">

**Author:** [@serjooo](https://forums.swift.org/u/serjooo)\
**Replies:** 6\
**Last updated:** [March 3, 2021, 12:36pm UTC](https://forums.swift.org/t/set-uniqueness-of-instances-with-the-same-hashvalue/45235 "2021-03-03T12:36:57Z")

</div>

I stumbled on an unexpected behavior of Sets and Dictionaries while conforming to Hashable. I am wondering how are Sets and Dictionarys expected to behave for values with the same hashValue, but that aren't equatable? G…

---

## [Meaning of 'must' in \`Hashable\` documentation](https://forums.swift.org/t/meaning-of-must-in-hashable-documentation/41544)

<div class="topic-metadata">

**Author:** [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Replies:** 3\
**Last updated:** [October 28, 2020, 10:08pm UTC](https://forums.swift.org/t/meaning-of-must-in-hashable-documentation/41544 "2020-10-28T22:08:35Z")

</div>

In the documentation for Hashable.hash(into:) we currently have the following line: The components used for hashing must be the same as the components compared in your type’s == operator implementation. IIRC, back wh…

---

## [Hashable.hash(into:) problem on iOS 9](https://forums.swift.org/t/hashable-hash-into-problem-on-ios-9/39390)

<div class="topic-metadata">

**Author:** [@mman](https://forums.swift.org/u/mman)\
**Replies:** 4\
**Last updated:** [August 14, 2020, 5:37pm UTC](https://forums.swift.org/t/hashable-hash-into-problem-on-ios-9/39390 "2020-08-14T17:37:36Z")

</div>

Hi guys, I am using custom implementation of Hashable protocol on my classes using fairly primitive logic using Hasher.combine() on several String/Int based fields. Everything seems to be working really well on Linux, …

---

## [Do I have to re-define equality/hashing for NSObject sub-classes?](https://forums.swift.org/t/do-i-have-to-re-define-equality-hashing-for-nsobject-sub-classes/35529)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 0\
**Last updated:** [April 20, 2020, 7:45pm UTC](https://forums.swift.org/t/do-i-have-to-re-define-equality-hashing-for-nsobject-sub-classes/35529 "2020-04-20T19:45:48Z")

</div>

I forgot; are sub-classes automatically marked as conforming to their base classes protocols? Even if they would do the wrong thing? I'm guessing "yes" to both questions. For any of the protocols that can have automat…

---

## [Hashable not auto-implementing when a member has a protocol type whose conformers must be Hashable](https://forums.swift.org/t/hashable-not-auto-implementing-when-a-member-has-a-protocol-type-whose-conformers-must-be-hashable/33965)

<div class="topic-metadata">

**Author:** [@Zanchee](https://forums.swift.org/u/Zanchee)\
**Replies:** 12\
**Last updated:** [February 21, 2020, 3:29pm UTC](https://forums.swift.org/t/hashable-not-auto-implementing-when-a-member-has-a-protocol-type-whose-conformers-must-be-hashable/33965 "2020-02-21T15:29:20Z")

</div>

I'm trying to implement a protocol that is restricted to UIViewControllers. I have a struct that has an array of this protocol, and it isn't able to infer Hashable even though it is able to infer Hashable if the array t…

---

## [Hashing Weak Variables](https://forums.swift.org/t/hashing-weak-variables/31345)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 19\
**Last updated:** [December 5, 2019, 9:01pm UTC](https://forums.swift.org/t/hashing-weak-variables/31345 "2019-12-05T21:01:13Z")

</div>

I'm trying to make a wrapper (property delegate) that would encapsulate how weak references should be compared and hashed. Consider this example: class MyClass { } struct WeakRef: Hashable { weak var ref: MyClass?…

---

## [PrePitch: Memberwise protocol requirement synthesis for structs](https://forums.swift.org/t/prepitch-memberwise-protocol-requirement-synthesis-for-structs/26202)

<div class="topic-metadata">

**Author:** [@owenv](https://forums.swift.org/u/owenv)\
**Replies:** 2\
**Last updated:** [June 23, 2019, 11:55pm UTC](https://forums.swift.org/t/prepitch-memberwise-protocol-requirement-synthesis-for-structs/26202 "2019-06-23T23:55:55Z")

</div>

After seeing lots of discussion on the forums recently about what a general approach to protocol requirement synthesis might look like, I decided to explore a few ideas. The following is a (very early/rough) look at how …

---

## [Concrete Type Conformance to 'Hashable' Protocol](https://forums.swift.org/t/concrete-type-conformance-to-hashable-protocol/20443)

<div class="topic-metadata">

**Author:** [@achtenhagen](https://forums.swift.org/u/achtenhagen)\
**Replies:** 4\
**Last updated:** [February 14, 2019, 2:53pm UTC](https://forums.swift.org/t/concrete-type-conformance-to-hashable-protocol/20443 "2019-02-14T14:53:29Z")

</div>

Hi all, I am trying to define a Word protocol which is then adopted by each type of word (adjective, adverb, noun, pronoun and verb). My goal is to have a dictionary support subscripting so that it accepts any type that…

---

## [Uniquing collections efficiently](https://forums.swift.org/t/uniquing-collections-efficiently/19457)

<div class="topic-metadata">

**Author:** [@ASwiftUser](https://forums.swift.org/u/ASwiftUser)\
**Replies:** 7\
**Last updated:** [February 2, 2019, 9:45pm UTC](https://forums.swift.org/t/uniquing-collections-efficiently/19457 "2019-02-02T21:45:01Z")

</div>

It often comes up that you want to unique a collection, that is create a new collection with no duplicate elements. However, I often see people do something like this: var uniqued = \[Int\]() for element in myArray { if…

---

## [How is synthesized Hashable implemented?](https://forums.swift.org/t/how-is-synthesized-hashable-implemented/15960)

<div class="topic-metadata">

**Author:** [@kelin](https://forums.swift.org/u/kelin)\
**Replies:** 3\
**Last updated:** [September 10, 2018, 4:19pm UTC](https://forums.swift.org/t/how-is-synthesized-hashable-implemented/15960 "2018-09-10T16:19:25Z")

</div>

What hash function it uses and how does it calculate hashValue for enum's with associated values and structs?

---

## [Hashable struct in function](https://forums.swift.org/t/hashable-struct-in-function/15705)

<div class="topic-metadata">

**Author:** [@Max](https://forums.swift.org/u/Max)\
**Replies:** 10\
**Last updated:** [August 31, 2018, 8:25pm UTC](https://forums.swift.org/t/hashable-struct-in-function/15705 "2018-08-31T20:25:36Z")

</div>

Hello ! I would like to have some clarifications about why Set does not interact properly with a Hashable struct declared in a function. Here I have a struct Hello that conforms to hashable and only needs its id for the…
