Skip to content

Instantly share code, notes, and snippets.

@kathgironpe
Forked from scotteg/HashableType.swift
Created September 11, 2016 05:49
Show Gist options
  • Save kathgironpe/42ffef84b10334a27a10bfc4ed2d7127 to your computer and use it in GitHub Desktop.
Save kathgironpe/42ffef84b10334a27a10bfc4ed2d7127 to your computer and use it in GitHub Desktop.
A Swift HashableType that enables nesting a Set of any Hashable type within a Set
func ==(x: HashableType, y: HashableType) -> Bool {
return x.isEqual(y.value)
}
struct HashableType: Hashable {
let value: Any
var hashValue: Int {
return getHashValue()
}
let getHashValue: () -> Int
let isEqual: (Any) -> Bool
init<T: Hashable>(_ value: T) {
self.value = value
getHashValue = { value.hashValue }
isEqual = {
if let otherValue = $0 as? T {
return value == otherValue
}
return false
}
}
}
let set1: Set = [HashableType("a")]
let set2: Set = [HashableType(1)]
let set3: Set = [HashableType(true)]
let mixedSet: Set = [set1, set2, set3]
for innerSet in mixedSet {
for hashableType in innerSet {
print(hashableType.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment