Questions tagged [hashable]

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.

161 questions
2
votes
1 answer

Set of hashable custom elements inserts equal elements

Setup: I want to use a Set of the following struct: struct NumberPair: Hashable { let n1: Int let n2: Int static func == (lhs: NumberPair, rhs: NumberPair) -> Bool { lhs.n1 == rhs.n1 && lhs.n2 == rhs.n2 || lhs.n2 ==…
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
2
votes
1 answer

SwiftUI enum String, Codable, Hashable Error

import SwiftUI import Foundation import Combine import CoreLocation struct structuralCodes: Hashable, Codable, Identifiable { var id = UUID() var index: Int var title: String var icon: String var isFeatured: Bool var…
2
votes
2 answers

How detect hashable types with structural pattern matching?

Using structural pattern matching, how do you write a case that matches instances of hashable types? I've tried: for obj in [], (), set(), frozenset(), 10, None, dict(): match obj: case object(__hash__=_): print('Hashable…
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2
votes
2 answers

How to add the Hashable protocol to CLLocationCoordinate2D using an extension in SwiftUI

So I have a custom struct with one property of type String and another of type CLLocationCoordinate2D. Apparently, String conforms to Hashable and if I can extend CLLocationCoordinate2D to conform to Hashable, my custom struct will also be Hashable.…
nickcoding
  • 305
  • 8
  • 35
2
votes
2 answers

Immutable hashable list with correct typings

I need to use sequence of items as dict key and to feat List[...] type. If I use tuple, than it does not feat to a List[...] type and I cannot use Tuple[...] type, because tuple length is not known. Is there any class (maybe from a 3rd party…
Yaroslav Kishchenko
  • 475
  • 1
  • 4
  • 15
2
votes
1 answer

Swift - Can I make a protocol Hashable?

I have written a simple protocol Data : public protocol Data { var state: [String: Any] { get set } var objectId: String? { get set } } public extension Data { var objectId: String? { get { return…
Another Dude
  • 1,204
  • 4
  • 15
  • 33
2
votes
2 answers

Why don't functions preserve identity?

I was wondering why Python 3.7 functions behave in a rather strange way. I think it's kinda weird and contradictory to the whole notion of hashability. Let me clarify what I encounter with a simple example code. Knowing that tuples are hashable,…
2
votes
1 answer

Swift: Using ObjectID of class for hashable protocol results in random behaviour in set.contains method. What is wrong with the code?

I have a small number of instances of a custom class stored in a set. I need to check if a certain element is contained in that set. The criteria for a match must be the object's ID, not its content. For simplification assume a class with an integer…
MassMover
  • 529
  • 2
  • 17
2
votes
1 answer

Cache results of Swift hash(into:) Hashable protocol requirement

I have a class being heavily used in Sets and Dictionaries. For performance reasons this class implements Hashable in a old way and caches the computed hash: let hashValue: Int init(...) { self.hashValue = ... } In Xcode 10.2 I see a warning,…
Tim
  • 1,877
  • 19
  • 27
2
votes
1 answer

Why do set operators work with dict_key view objects but not the equivalent set methods?

Edit: Possible duplicate. Only after posting this question and looking in "related questions" was I able to find Why are set methods like .intersection() not supported on set-like objects?, this question may similar enough to be a duplicate. I am…
johnDanger
  • 1,990
  • 16
  • 22
2
votes
1 answer

Swift - A Decimal's hashValue is the same for X == -X, cannot be used for comparing hashValues

We found out that you cannot distinguish two Decimals by their hashValue if one is the negative of the other. We use Decimals as a field in a struct and that struct implements Hashable to be able to be put in a set. Our business logic then requires…
Simon
  • 470
  • 1
  • 7
  • 22
2
votes
2 answers

Override Equatable and Hashable in Swift

Is there a way to override Equatable of NSManagedObject? I have a coredata dataset of 300k objects and I need to remove duplicates base on the object's business id. // Coredata NSManagedObject import Foundation import…
Unikorn
  • 1,140
  • 1
  • 13
  • 27
2
votes
1 answer

How can I update this Hashable.hashValue to conform to new requirements.?

I'm trying to fix up an old tutorial from RayWenderlich's site, no longer supported. The warning appears in three files, Chain.swift, Cookie.swift and Swap.swift from the "How to Make a Game Like Candy Crush with SpriteKit and Swift:" tutorial I'm…
Harry McGovern
  • 517
  • 5
  • 19
2
votes
0 answers

The Hashable now want to use protocol instead of hashValue but I can't figure it out

I use to have the following code that worked in Swift 4.2 but is now deprecated in Swift 5: struct xxx: Hashable { var hashValue: Int {return uniqueIdentifier} When I try to use the new hash(into hasher: inout Hasher) I'm not sure what to do.…
mretondo
  • 187
  • 1
  • 9
2
votes
1 answer

Why does struct need to conform to Hashable as well as Generic array when converting to a dictionary

The goal was to convert items in a generic list into a dictionary for the variable uniqueKeys, but I saw the error: Cannot subscript a value of incorrect or ambiguous type I knew something needed to conform to the Hashable protocol and eventually…
Turnipdabeets
  • 5,815
  • 8
  • 40
  • 63