-
-
Save sadlerjw/2cc16b4375b02fe7f400 to your computer and use it in GitHub Desktop.
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
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