Last active
December 16, 2016 15:15
-
-
Save scotteg/3812436a0ede5777cb8f 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 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