Skip to content

Commit 31e9807

Browse files
authored
Update AnyHashable documentation
1 parent 423bcdf commit 31e9807

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

stdlib/public/core/AnyHashable.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,33 @@ internal struct _ConcreteHashableBox<Base: Hashable>: _AnyHashableBox {
107107
/// A type-erased hashable value.
108108
///
109109
/// 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.
111111
///
112112
/// You can store mixed-type keys in dictionaries and other collections that
113113
/// require `Hashable` conformance by wrapping mixed-type keys in
114114
/// `AnyHashable` instances:
115115
///
116116
/// let descriptions: [AnyHashable: Any] = [
117-
/// AnyHashable("😄"): "emoji",
118117
/// AnyHashable(42): "an Int",
119118
/// AnyHashable(Int8(43)): "an Int8",
120119
/// AnyHashable(Set(["a", "b"])): "a set of strings"
121120
/// ]
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"
124123
/// print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
124+
/// print(descriptions[AnyHashable(44)]) // prints "nil"
125125
/// 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"
126137
@frozen
127138
public struct AnyHashable {
128139
internal var _box: _AnyHashableBox
@@ -189,12 +200,22 @@ public struct AnyHashable {
189200

190201
extension AnyHashable: Equatable {
191202
/// Returns a Boolean value indicating whether two type-erased hashable
192-
/// instances wrap the same type and value.
203+
/// instances wrap the same value.
193204
///
194205
/// Two instances of `AnyHashable` compare as equal if and only if the
195206
/// underlying types have the same conformance to the `Equatable` protocol
196207
/// and the underlying values compare as equal.
197208
///
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+
///
198219
/// - Parameters:
199220
/// - lhs: A type-erased hashable value.
200221
/// - rhs: Another type-erased hashable value.

0 commit comments

Comments
 (0)