Skip to content

Instantly share code, notes, and snippets.

@scotteg
Last active December 16, 2016 15:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save scotteg/3812436a0ede5777cb8f to your computer and use it in GitHub Desktop.
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
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