@@ -107,22 +107,33 @@ internal struct _ConcreteHashableBox<Base: Hashable>: _AnyHashableBox {
107
107
/// A type-erased hashable value.
108
108
///
109
109
/// The `AnyHashable` type forwards equality comparisons and hashing operations
110
- /// to an underlying hashable value, hiding its specific underlying type .
110
+ /// to an underlying hashable value, hiding the type of the wrapped value .
111
111
///
112
112
/// You can store mixed-type keys in dictionaries and other collections that
113
113
/// require `Hashable` conformance by wrapping mixed-type keys in
114
114
/// `AnyHashable` instances:
115
115
///
116
116
/// let descriptions: [AnyHashable: Any] = [
117
- /// AnyHashable("😄"): "emoji",
118
117
/// AnyHashable(42): "an Int",
119
118
/// AnyHashable(Int8(43)): "an Int8",
120
119
/// AnyHashable(Set(["a", "b"])): "a set of strings"
121
120
/// ]
122
- /// print(descriptions[AnyHashable(42)]!) // prints "an Int"
123
- /// print(descriptions[AnyHashable(45)]) // prints "nil "
121
+ /// print(descriptions[AnyHashable(42)]!) // prints "an Int"
122
+ /// print(descriptions[AnyHashable(Int8(42))]!) // prints "an Int "
124
123
/// print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
124
+ /// print(descriptions[AnyHashable(44)]) // prints "nil"
125
125
/// print(descriptions[AnyHashable(Set(["a", "b"]))]!) // prints "a set of strings"
126
+ ///
127
+ /// If the wrapped value is of a type that can be bridged using `as` to a
128
+ /// different but compatible class defined in Foundation, or vice versa, then
129
+ /// the underlying hashable value to which operations are forwarded might not be
130
+ /// of the wrapped type itself but of a transitively bridged type:
131
+ ///
132
+ /// let x: AnyHashable = [1, 2, 3] as NSArray
133
+ /// let y: AnyHashable = [1, 2, 3] as [Double]
134
+ /// let z: AnyHashable = [1, 2, 3] as [Int8]
135
+ /// print(x == y, x.hashValue == y.hashValue) // prints "true true"
136
+ /// print(y == z, y.hashValue == z.hashValue) // prints "true true"
126
137
@frozen
127
138
public struct AnyHashable {
128
139
internal var _box : _AnyHashableBox
@@ -189,12 +200,22 @@ public struct AnyHashable {
189
200
190
201
extension AnyHashable : Equatable {
191
202
/// Returns a Boolean value indicating whether two type-erased hashable
192
- /// instances wrap the same type and value.
203
+ /// instances wrap the same value.
193
204
///
194
205
/// Two instances of `AnyHashable` compare as equal if and only if the
195
206
/// underlying types have the same conformance to the `Equatable` protocol
196
207
/// and the underlying values compare as equal.
197
208
///
209
+ /// If the wrapped values are of a type that can be bridged using `as` to a
210
+ /// different but compatible class defined in Foundation, or vice versa, then
211
+ /// the underlying values compared might not be of the wrapped type itself but
212
+ /// of a transitively bridged type:
213
+ ///
214
+ /// let a = (42 as NSNumber as AnyHashable)
215
+ /// let b = (42 as Double as AnyHashable)
216
+ /// let c = (42 as Int8 as AnyHashable)
217
+ /// print(a == b, b == c, a == c) // prints "true true true"
218
+ ///
198
219
/// - Parameters:
199
220
/// - lhs: A type-erased hashable value.
200
221
/// - rhs: Another type-erased hashable value.
0 commit comments