Skip to content

Instantly share code, notes, and snippets.

@sadlerjw
Last active March 5, 2016 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadlerjw/2cc16b4375b02fe7f400 to your computer and use it in GitHub Desktop.
Save sadlerjw/2cc16b4375b02fe7f400 to your computer and use it in GitHub Desktop.
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