This Hashable package defines a class, Hashable, for types that can be converted to a hash value. This class exists for the benefit of hashing-based data structures. The package provides instances for basic types and a way to combine hash values.
Questions tagged [hashable]
161 questions
6
votes
2 answers
How does Set ensure equatability in Swift?
I'm reading Set
You use a set instead of an array when you need to test efficiently for membership and you aren’t concerned with the order of the elements in the collection, or when you need to ensure that each element appears only once in a…

onmyway133
- 45,645
- 31
- 257
- 263
6
votes
3 answers
Swift: Hashable struct with dictionary property
I have a struct in Swift that looks like this:
internal struct MapKey {
internal let id: String
internal let values: [String:String]
}
extension MapKey: Equatable {}
func ==(lhs: MapKey, rhs: MapKey) -> Bool {
return lhs.id == rhs.id &&…

unbekant
- 1,555
- 22
- 31
6
votes
1 answer
Why does Data.HashTable use hashing with salt (from Data.Hashable)?
I do not understand why Data.HashTable is using Data.Hashable , which has hashWithSalt as the (only/basic) method.
This does not fit with the natural optimization of computing the hash value once, and storing it in the object (natural, because…

d8d0d65b3f7cf42
- 2,597
- 15
- 28
5
votes
1 answer
SWIFTUI Call Key Dictionary not work with the error: 'Subscript index of type '() -> Bool' in a key path must be Hashable'
I have this view:
import SwiftUI
struct SectionView1: View {
let dateStr:String
@Binding var isSectionView:Bool
var body: some View {
HStack {
Button(action: {
self.isSectionView.toggle()
…

Cesare Piersigilli
- 377
- 3
- 10
5
votes
3 answers
Swift 5: How to make a Set containing Class Types (for NSXPCInterface)
In Apple documentation of NSXPCInterface.setClasses(_:for:argumentIndex:ofReply:), for Swift, the first parameter is described as:
An NSSet containing Class objects —for example, [MyObject class].
Hmmm, it looks as though someone neglected to update…

Jerry Krinock
- 4,860
- 33
- 39
5
votes
1 answer
Make simple tuple conform to Hashable, so can be a Dictionary Key
I'd like to use a very simple tuple as a key:
(Int, Int)
Dictionary keys need to be Hashable. I've learnt.
But can't find how I make this simple tuple Hashable, and do struggle with protocol conformance at the best of times.
More profoundly, a…

Confused
- 6,048
- 6
- 34
- 75
5
votes
1 answer
What's the order of __hash__ and __eq__ evaluation for a Python dict?
I'm trying to understand what Python dictionaries must be doing internally to locate a key. It seems to me that hash would be evaluated first, and if there's a collision, Python would iterate through the keys till it finds one for which eq returns…

Vineet Bansal
- 491
- 1
- 4
- 14
4
votes
1 answer
NamedTuples, Hashable and Python
Consider the following code:
#!/usr/bin/env python3.7
from typing import NamedTuple, Set
class Person(NamedTuple):
name: str
fred: Set[str]
p = Person("Phil", set())
print(p)
my_dict = {}
my_dict[p] = 10
print(my_dict)
which produces…

Phil Lord
- 2,917
- 1
- 20
- 31
4
votes
2 answers
How to conform custom class with optional properties to "hashable" protocol
Suppose I have a base class "Person" that I want to add to a Set (List) and need to therefore conform to Hashable and Equatable:
class Person : Equatable, Hashable {
let firstName: String
let lastName: String
var nickname: String?
let dateOfBirth:…

BrandonLenz
- 103
- 9
4
votes
2 answers
Automatically making a class hashable
There are several standard ways to make a class hashable, for example (borrowing from SO):
# assume X has 2 attributes: attr_a and attr_b
class X:
def __key(self):
return (self.attr_a, self.attr_b)
def __eq__(x, y):
return isinstance(y,…

max
- 49,282
- 56
- 208
- 355
3
votes
3 answers
Stored property type 'CGPoint' does not conform to protocol 'Hashable',
Imagine a card board. All cards are on a view I call CardBoard.
All cards are on an array:
var cards:[Card]
Every card has its own position.
This is Card
struct Card: View {
let id = UUID()
var name:String
var code:String
var…

Duck
- 34,902
- 47
- 248
- 470
3
votes
1 answer
Check if Any conforms to Hashable and get hash value
I want to get the hash value of an Any object that conforms to Hashable.
However, with this code:
let anyValue: Any
//...
if let h = anyValue as? Hashable {
return h.hashValue
}
I'm getting this error
Protocol 'Hashable'…

Peter Lapisu
- 19,915
- 16
- 123
- 179
3
votes
2 answers
Is it possible to use a Type as a dictionary key in Swift?
I’m making a Farm where everything that can be grown conforms to Growable protocol. When you plant a plant, you call this func:
myFarm.planting(qty: Int, of: T.Type) -> Farm
Now I want each instance of Farm to have a dictionary instance…

CommaToast
- 11,370
- 7
- 54
- 69
3
votes
1 answer
performance considerations of hashValue in swift protocol Hashable
Besides being a unique integer are there any performance considerations for the selection of a hashValue in a swift Hashable Type that may be inserted into a Set? For instance will the size of the integer values I choose affect the size of the…

gloo
- 2,490
- 3
- 22
- 38
3
votes
1 answer
Python : Argument based Singleton
I'm following this link and trying to make a singleton class. But, taking arguments (passed while initiating a class) into account so that the same object is returned if the arguments are same.
So, instead of storing class name/class reference as a…

Pankaj Singhal
- 15,283
- 9
- 47
- 86