Skip to content

Commit cf8dcde

Browse files
authored
Merge pull request #4230 from natecook1000/swift-3.0-branch
[swift-3.0-branch] [stdlib] Revise stdlib documentation comments
2 parents 1d76ecb + 081d0a9 commit cf8dcde

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1532
-781
lines changed

stdlib/public/core/Algorithm.swift

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Returns the lesser of `x` and `y`.
13+
/// Returns the lesser of two comparable values.
1414
///
15-
/// If `x == y`, returns `x`.
15+
/// - Parameters:
16+
/// - x: A value to compare.
17+
/// - y: Another value to compare.
18+
/// - Returns: The lesser of `x` and `y`. If `x` is equal to `y`, returns `x`.
1619
public func min<T : Comparable>(_ x: T, _ y: T) -> T {
1720
// In case `x == y` we pick `x`.
1821
// This preserves any pre-existing order in case `T` has identity,
@@ -23,7 +26,13 @@ public func min<T : Comparable>(_ x: T, _ y: T) -> T {
2326

2427
/// Returns the least argument passed.
2528
///
26-
/// If there are multiple equal least arguments, returns the first one.
29+
/// - Parameters:
30+
/// - x: A value to compare.
31+
/// - y: Another value to compare.
32+
/// - z: A third value to compare.
33+
/// - rest: Zero or more additional values.
34+
/// - Returns: The least of all the arguments. If there are multiple equal
35+
/// least arguments, the result is the first one.
2736
public func min<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
2837
var minValue = min(min(x, y), z)
2938
// In case `value == minValue`, we pick `minValue`. See min(_:_:).
@@ -33,17 +42,26 @@ public func min<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
3342
return minValue
3443
}
3544

36-
/// Returns the greater of `x` and `y`.
45+
/// Returns the greater of two comparable values.
3746
///
38-
/// If `x == y`, returns `y`.
47+
/// - Parameters:
48+
/// - x: A value to compare.
49+
/// - y: Another value to compare.
50+
/// - Returns: The greater of `x` and `y`. If `x` is equal to `y`, returns `y`.
3951
public func max<T : Comparable>(_ x: T, _ y: T) -> T {
4052
// In case `x == y`, we pick `y`. See min(_:_:).
4153
return y >= x ? y : x
4254
}
4355

4456
/// Returns the greatest argument passed.
4557
///
46-
/// If there are multiple equal greatest arguments, returns the last one.
58+
/// - Parameters:
59+
/// - x: A value to compare.
60+
/// - y: Another value to compare.
61+
/// - z: A third value to compare.
62+
/// - rest: Zero or more additional values.
63+
/// - Returns: The greatest of all the arguments. If there are multiple equal
64+
/// greatest arguments, the result is the last one.
4765
public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
4866
var maxValue = max(max(x, y), z)
4967
// In case `value == maxValue`, we pick `value`. See min(_:_:).
@@ -53,18 +71,20 @@ public func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T {
5371
return maxValue
5472
}
5573

56-
/// The iterator for `EnumeratedSequence`. `EnumeratedIterator`
57-
/// wraps a `Base` iterator and yields successive `Int` values,
58-
/// starting at zero, along with the elements of the underlying
59-
/// `Base`:
74+
/// The iterator for `EnumeratedSequence`.
75+
///
76+
/// An instance of `EnumeratedIterator` wraps a base iterator and yields
77+
/// successive `Int` values, starting at zero, along with the elements of the
78+
/// underlying base iterator. The following example enumerates the elements of
79+
/// an array:
6080
///
6181
/// var iterator = ["foo", "bar"].enumerated().makeIterator()
6282
/// iterator.next() // (0, "foo")
6383
/// iterator.next() // (1, "bar")
6484
/// iterator.next() // nil
6585
///
66-
/// - Note: Idiomatic usage is to call `enumerate` instead of
67-
/// constructing an `EnumerateIterator` directly.
86+
/// To create an instance of `EnumeratedIterator`, call
87+
/// `enumerated().makeIterator()` on a sequence or collection.
6888
public struct EnumeratedIterator<
6989
Base : IteratorProtocol
7090
> : IteratorProtocol, Sequence {
@@ -91,14 +111,22 @@ public struct EnumeratedIterator<
91111
}
92112
}
93113

94-
/// The type of the `enumerated()` property.
114+
/// An enumeration of the elements of a sequence or collection.
115+
///
116+
/// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s are
117+
/// consecutive `Int` values starting at zero, and *x*s are the elements of a
118+
/// base sequence.
95119
///
96-
/// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s
97-
/// are consecutive `Int`s starting at zero, and *x*s are the elements
98-
/// of a `Base` `Sequence`:
120+
/// To create an instance of `EnumeratedSequence`, call `enumerated()` on a
121+
/// sequence or collection. The following example enumerates the elements of
122+
/// an array.
99123
///
100124
/// var s = ["foo", "bar"].enumerated()
101-
/// Array(s) // [(0, "foo"), (1, "bar")]
125+
/// for (n, x) in s {
126+
/// print("\(n): \(x)")
127+
/// }
128+
/// // Prints "0: foo"
129+
/// // Prints "1: bar"
102130
public struct EnumeratedSequence<Base : Sequence> : Sequence {
103131
internal var _base: Base
104132

@@ -108,8 +136,6 @@ public struct EnumeratedSequence<Base : Sequence> : Sequence {
108136
}
109137

110138
/// Returns an iterator over the elements of this sequence.
111-
///
112-
/// - Complexity: O(1).
113139
public func makeIterator() -> EnumeratedIterator<Base.Iterator> {
114140
return EnumeratedIterator(_base: _base.makeIterator())
115141
}

stdlib/public/core/AnyHashable.swift

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ internal struct _ConcreteHashableBox<Base : Hashable> : _AnyHashableBox {
8787

8888
/// A type-erased hashable value.
8989
///
90-
/// Forwards equality comparisons and hashing operations to an
91-
/// underlying hashable value, hiding its specific type.
90+
/// The `AnyHashable` type forwards equality comparisons and hashing operations
91+
/// to an underlying hashable value, hiding its specific underlying type.
9292
///
93-
/// You can store mixed-type keys in `Dictionary` and other
94-
/// collections that require `Hashable` by wrapping mixed-type keys in
93+
/// You can store mixed-type keys in dictionaries and other collections that
94+
/// require `Hashable` conformance by wrapping mixed-type keys in
9595
/// `AnyHashable` instances:
9696
///
97-
/// let descriptions: [AnyHashable : Any] = [
97+
/// let descriptions: [AnyHashable: Any] = [
9898
/// AnyHashable("😄"): "emoji",
9999
/// AnyHashable(42): "an Int",
100100
/// AnyHashable(Int8(43)): "an Int8",
@@ -107,17 +107,24 @@ internal struct _ConcreteHashableBox<Base : Hashable> : _AnyHashableBox {
107107
public struct AnyHashable {
108108
internal var _box: _AnyHashableBox
109109

110-
/// Creates an opaque hashable value that wraps `base`.
110+
/// Creates a type-erased hashable value that wraps the given instance.
111111
///
112-
/// Example:
112+
/// The following example creates two type-erased hashable values: `x` wraps
113+
/// an `Int` with the value 42, while `y` wraps a `UInt8` with the same
114+
/// numeric value. Because the underlying types of `x` and `y` are
115+
/// different, the two variables do not compare as equal despite having
116+
/// equal underlying values.
113117
///
114118
/// let x = AnyHashable(Int(42))
115119
/// let y = AnyHashable(UInt8(42))
116120
///
117-
/// print(x == y) // Prints "false" because `Int` and `UInt8`
118-
/// // are different types.
121+
/// print(x == y)
122+
/// // Prints "false" because `Int` and `UInt8` are different types
119123
///
120-
/// print(x == AnyHashable(Int(42))) // Prints "true".
124+
/// print(x == AnyHashable(Int(42)))
125+
/// // Prints "true"
126+
///
127+
/// - Parameter base: A hashable value to wrap.
121128
public init<H : Hashable>(_ base: H) {
122129
if let customRepresentation =
123130
(base as? _HasCustomAnyHashableRepresentation)?._toCustomAnyHashable() {
@@ -135,11 +142,16 @@ public struct AnyHashable {
135142
self._box = _ConcreteHashableBox(base)
136143
}
137144

138-
/// The value wrapped in this `AnyHashable` instance.
145+
/// The value wrapped by this instance.
146+
///
147+
/// The `base` property can be cast back to its original type using one of
148+
/// the casting operators (`as?`, `as!`, or `as`).
139149
///
140-
/// let anyMessage = AnyHashable("Hello")
141-
/// let unwrappedMessage: Any = anyMessage.base
142-
/// print(unwrappedMessage) // prints "hello"
150+
/// let anyMessage = AnyHashable("Hello world!")
151+
/// if let unwrappedMessage = anyMessage.base as? String {
152+
/// print(unwrappedMessage)
153+
/// }
154+
/// // Prints "Hello world!"
143155
public var base: Any {
144156
return _box._base
145157
}
@@ -155,6 +167,31 @@ public struct AnyHashable {
155167
}
156168

157169
extension AnyHashable : Equatable {
170+
/// Returns a Boolean value indicating whether two type-erased hashable
171+
/// instances wrap the same type and value.
172+
///
173+
/// Two instances of `AnyHashable` compare as equal if and only if the
174+
/// underlying types have the same conformance to the `Equatable` protocol
175+
/// and the underlying values compare as equal.
176+
///
177+
/// The following example creates two type-erased hashable values: `x` wraps
178+
/// an `Int` with the value 42, while `y` wraps a `UInt8` with the same
179+
/// numeric value. Because the underlying types of `x` and `y` are
180+
/// different, the two variables do not compare as equal despite having
181+
/// equal underlying values.
182+
///
183+
/// let x = AnyHashable(Int(42))
184+
/// let y = AnyHashable(UInt8(42))
185+
///
186+
/// print(x == y)
187+
/// // Prints "false" because `Int` and `UInt8` are different types
188+
///
189+
/// print(x == AnyHashable(Int(42)))
190+
/// // Prints "true"
191+
///
192+
/// - Parameters:
193+
/// - lhs: A type-erased hashable value.
194+
/// - rhs: Another type-erased hashable value.
158195
public static func == (lhs: AnyHashable, rhs: AnyHashable) -> Bool {
159196
return lhs._box._isEqual(to: rhs._box)
160197
}

0 commit comments

Comments
 (0)