-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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