Skip to content

Instantly share code, notes, and snippets.

@sadlerjw
Last active Mar 5, 2016
Embed
What would you like to do?
protocol AnyEquatable {
func equals(otherObject: AnyEquatable) -> Bool
}
extension AnyEquatable where Self : Equatable {
// otherObject could also be 'Any'
func equals(otherObject: AnyEquatable) -> Bool {
if let otherAsSelf = otherObject as? Self {
return otherAsSelf == self
}
return false
}
}
extension Int : AnyEquatable { }
extension String : AnyEquatable { }
extension Array where Element : AnyEquatable {
func indexOfElement(element : Element) -> Index? {
return indexOf({ (currentElement : Element ) -> Bool in
element.equals(currentElement)
})
}
}
let one = 1
let two = "two"
let three = 3
let array : [AnyEquatable] = [one, two, three]
array.indexOfElement(two) // ERROR: Using 'AnyEquatable' as a concrete type conforming to protocol 'AnyEquatable' is not supported
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment