[swift-users] Using ... as a concrete type conforming to protocol ... is not supported
Hooman Mehr
hooman at mac.com
Fri Mar 25 19:27:36 CDT 2016
> On Mar 25, 2016, at 2:51 PM, Jason Sadler via swift-users <swift-users at 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160325/f2f37e93/attachment.html>
More information about the swift-users
mailing list