How to Implement hash(into:) from hashValue in Swift?
You can simply use hasher.combine and call it with the values you want to use for hashing: func hash(into hasher: inout Hasher) { hasher.combine(index) hasher.combine(title) }
You can simply use hasher.combine and call it with the values you want to use for hashing: func hash(into hasher: inout Hasher) { hasher.combine(index) hasher.combine(title) }
You’re missing the declaration: struct DateStruct: Hashable { And your == function is wrong. You should compare the three properties. static func == (lhs: DateStruct, rhs: DateStruct) -> Bool { return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day } It’s possible for two different values to have the same hash value.
Instead of using a custom hashable dictionary, use this and avoid reinventing the wheel! It’s a frozen dictionary that’s all hashable. https://pypi.org/project/frozendict/ Code: from frozendict import frozendict def freezeargs(func): “””Transform mutable dictionnary Into immutable Useful to be compatible with cache “”” @functools.wraps(func) def wrapped(*args, **kwargs): args = tuple([frozendict(arg) if isinstance(arg, dict) else arg for arg … Read more
Info on Enumerations as dictionary keys: From the Swift book: Enumeration member values without associated values (as described in Enumerations) are also hashable by default. However, your Enumeration does have a member value with an associated value, so Hashable conformance has to be added manually by you. Solution The problem with your implementation, is that … Read more
1) Keys must not be mutable, unless you have a user-defined class that is hashable but also mutable. That’s all that’s forced upon you. However, using a hashable, mutable object as a dict key might be a bad idea. 2) By not sharing values between the two dicts. It’s OK to share the keys, because … Read more
There’s a good article on the topic in the Python wiki: Why Lists Can’t Be Dictionary Keys. As explained there: What would go wrong if Python allowed using lists as keys, say, using their memory location as the hash? It would cause some unexpected behavior. Lists are generally treated as if their value was derived … Read more
Simply return dbName.hashValue from your hashValue function. FYI – the hash value does not need to be unique. The requirement is that two objects that equate equal must also have the same hash value. struct PetInfo: Hashable { var petName: String var dbName: String var hashValue: Int { return dbName.hashValue } static func == (lhs: … Read more
As the warning says, now you should implement the hash(into:) function instead. func hash(into hasher: inout Hasher) { switch self { case .mention: hasher.combine(-1) case .hashtag: hasher.combine(-2) case .url: hasher.combine(-3) case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable } } It would be even better (in case of … Read more
Simply setting the __hash__ method to that of the tuple class is not enough. You haven’t actually told it how to hash any differently. tuples are hashable because they are immutable. If you really wanted to make you specific example work, it might be like this: class X2(list): def __hash__(self): return hash(tuple(self)) In this case … Read more
There’s a good article on the topic in the Python wiki: Why Lists Can’t Be Dictionary Keys. As explained there: What would go wrong if you tried to use lists as keys, with the hash as, say, their memory location? It can be done without really breaking any of the requirements, but it leads to … Read more