> On Mar 25, 2016, at 2:51 PM, Jason Sadler via swift-users 
> <swift-users@swift.org> wrote:
> 
> (My particular use case can be seen here: 
> https://gist.github.com/sadlerjw/2cc16b4375b02fe7f400 
> <https://gist.github.com/sadlerjw/2cc16b4375b02fe7f400> … and the best 
> information I’ve been able to find on this so far is here: 
> http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself/33524927#33524927
>  
> <http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself/33524927#33524927>)

Here is the best you can do for your particular use case:

protocol AnyEquatable { func equals(other: Any) -> Bool }

func ==<T: Equatable>(lhs: T, rhs: Any) -> Bool {
    
    if let rhs = rhs as? T { return lhs == rhs } else { return false }
}

extension Bool:   AnyEquatable { func equals(other: Any) -> Bool { return self 
== other } }

extension Int:    AnyEquatable { func equals(other: Any) -> Bool { return self 
== other } }

extension Double: AnyEquatable { func equals(other: Any) -> Bool { return self 
== other } }

extension String: AnyEquatable { func equals(other: Any) -> Bool { return self 
== other } }

extension Array {
    
    func indexOfAny(element : AnyEquatable) -> Index? { return indexOf { 
element.equals($0) } }
}

var array: [Any] = [false, 1, 2.0, "three"]

array.indexOfAny(2.0)

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to