Skip to content

Instantly share code, notes, and snippets.

@thelbane
Created August 6, 2017 22:19
Show Gist options
  • Save thelbane/dfc94e6a8a37ae0ba004f6c2914e30d6 to your computer and use it in GitHub Desktop.
Save thelbane/dfc94e6a8a37ae0ba004f6c2914e30d6 to your computer and use it in GitHub Desktop.
import Foundation
enum ColumnSelection<Item> where Item: Hashable {
case viewAll
case selection(item: Item)
}
extension ColumnSelection: Hashable {
// MARK: - Hashable
var hashValue: Int {
switch self {
case .viewAll: return 0
case .selection(let item): return item.hashValue + 1
}
}
// MARK: - Equatable
static func ==(lhs: ColumnSelection<Item>, rhs: ColumnSelection<Item>) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
@thelbane
Copy link
Author

Gross. I thought I was being clever with the item.hashValue + 1 in line 17 to avoid a hash collision with .viewAll. However, hashValue is an Int (not UInt) therefore I could still collide with a hash value of -1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment