Skip to content

Commit 06f12fc

Browse files
authored
Incorporate further review comments
1 parent 4ee0cc1 commit 06f12fc

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

stdlib/public/core/AnyHashable.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,31 @@ internal struct _ConcreteHashableBox<Base: Hashable>: _AnyHashableBox {
109109
/// The `AnyHashable` type forwards equality comparisons and hashing operations
110110
/// to an underlying hashable value, hiding the type of the wrapped value.
111111
///
112-
/// For types that support conversions between each other using `as` or `as?`
113-
/// (such as `Int` and `NSNumber`), `AnyHashable` treats their values
114-
/// equivalently when type-erased by forwarding operations to canonical
115-
/// representations of the wrapped values.
112+
/// Where conversion using `as` or `as?` is possible between two types (such as
113+
/// `Int` and `NSNumber`), `AnyHashable` uses a canonical representation of the
114+
/// type-erased value so that instances wrapping the same value of either type
115+
/// compare as equal. For example, `AnyHashable(42)` compares as equal to
116+
/// `AnyHashable(42 as NSNumber)`.
116117
///
117118
/// You can store mixed-type keys in dictionaries and other collections that
118119
/// require `Hashable` conformance by wrapping mixed-type keys in
119120
/// `AnyHashable` instances:
120121
///
121122
/// let descriptions: [AnyHashable: Any] = [
122-
/// AnyHashable(42): "an Int",
123-
/// AnyHashable(Int8(43)): "an Int8",
124-
/// AnyHashable(Set(["a", "b"])): "a set of strings"
123+
/// 42: "an Int",
124+
/// 43 as Int8: "an Int8",
125+
/// ["a", "b"] as Set: "a set of strings"
125126
/// ]
126-
/// print(descriptions[AnyHashable(42)]!) // prints "an Int"
127-
/// print(descriptions[AnyHashable(Int8(42))]!) // prints "an Int"
128-
/// print(descriptions[AnyHashable(Int8(43))]!) // prints "an Int8"
129-
/// print(descriptions[AnyHashable(44)]) // prints "nil"
130-
/// print(descriptions[AnyHashable(Set(["a", "b"]))]!) // prints "a set of strings"
127+
/// print(descriptions[42]!) // prints "an Int"
128+
/// print(descriptions[42 as Int8]!) // prints "an Int"
129+
/// print(descriptions[43 as Int8]!) // prints "an Int8"
130+
/// print(descriptions[44]) // prints "nil"
131+
/// print(descriptions[["a", "b"] as Set]!) // prints "a set of strings"
131132
///
132-
/// Note that `AnyHashable` instances are not guaranteed to preserve the hash
133-
/// encoding of their wrapped values, even in cases where hash values appear to
134-
/// match in a particular release of the standard library.
133+
/// Note that `AnyHashable` does not guarantee that it preserves the hash
134+
/// encoding of wrapped values. Do not rely on `AnyHashable` generating such
135+
/// compatible hashes, as the hash encoding that it uses may change between any
136+
/// two releases of the standard library.
135137
@frozen
136138
public struct AnyHashable {
137139
internal var _box: _AnyHashableBox
@@ -198,13 +200,13 @@ public struct AnyHashable {
198200

199201
extension AnyHashable: Equatable {
200202
/// Returns a Boolean value indicating whether two type-erased hashable
201-
/// instances wrap the same underlying value.
203+
/// instances wrap the same value.
202204
///
203205
/// `AnyHashable` considers bridged counterparts (such as a `String` and an
204-
/// `NSString`) of the same value to be equivalent when type-erased. Where
205-
/// those compatible types have different definitions of equality comparisons,
206-
/// values that originally compare as not equal may then compare as equal when
207-
/// they are type-erased by conversion to `AnyHashable`:
206+
/// `NSString`) of the same value to be equivalent when type-erased. If those
207+
/// compatible types use different definitions for equality, values that were
208+
/// originally distinct might compare as equal when they are converted to
209+
/// `AnyHashable`:
208210
///
209211
/// let string1 = "café"
210212
/// let string2 = "cafe\u{301}" // U+301 COMBINING ACUTE ACCENT

0 commit comments

Comments
 (0)