You can also get a heterogenous dictionary, but it needs a type erasing helper box. With the following declarations:
public func ==<T: Equatable>(lhs: T, rhs: Any) -> Bool { if let rhs = rhs as? T { return lhs == rhs } else { return false } } public struct AnyKey : Hashable { public let hashValue: Int public let key: Any public let equals: (AnyKey) -> Bool init<T: Hashable>(_ key: T) { self.hashValue = key.hashValue self.key = key self.equals = { self.key as! T == $0.key } } } public func == (lhs: AnyKey, rhs: AnyKey) -> Bool { lhs.equals(rhs) } You can now create a heterogenous [AnyKey: Any] dictionary. Note that `AnyKey` is not very memory efficient. > On Mar 25, 2016, at 5:27 PM, Hooman Mehr <hoo...@mac.com> wrote: > > >> On Mar 25, 2016, at 2:51 PM, Jason Sadler via swift-users >> <swift-users@swift.org <mailto: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