Skip to content

Commit b6a0d9e

Browse files
committed
[stdlib] Documentation revisions
- Make RawRepresentable Codable abstracts distinguishable - Make the UnboundedRange example a little more user friendly - Correct the RangeReplaceableCollection example description - Revise CaseIterable discussion
1 parent ddea849 commit b6a0d9e

File tree

5 files changed

+92
-50
lines changed

5 files changed

+92
-50
lines changed

stdlib/public/core/Codable.swift.gyb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,8 @@ extension ${type} : Codable {
17671767
}
17681768

17691769
extension RawRepresentable where RawValue == ${type}, Self : Encodable {
1770-
/// Encodes this value into the given encoder.
1770+
/// Encodes this value into the given encoder, when the type's `RawValue`
1771+
/// is `${type}`.
17711772
///
17721773
/// This function throws an error if any values are invalid for the given
17731774
/// encoder's format.
@@ -1781,7 +1782,8 @@ extension RawRepresentable where RawValue == ${type}, Self : Encodable {
17811782
}
17821783

17831784
extension RawRepresentable where RawValue == ${type}, Self : Decodable {
1784-
/// Creates a new instance by decoding from the given decoder.
1785+
/// Creates a new instance by decoding from the given decoder, when the
1786+
/// type's `RawValue` is `${type}`.
17851787
///
17861788
/// This initializer throws an error if reading from the decoder fails, or
17871789
/// if the data read is corrupted or otherwise invalid.

stdlib/public/core/CollectionOfOne.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// An iterator that produces one or fewer instances of `Element`.
13+
/// An iterator that produces one or zero instances of an element.
14+
///
15+
/// `IteratorOverOne` is the iterator for the `CollectionOfOne` type.
1416
@_fixed_layout // FIXME(sil-serialize-all)
1517
public struct IteratorOverOne<Element> {
1618
@usableFromInline // FIXME(sil-serialize-all)
@@ -31,8 +33,8 @@ extension IteratorOverOne: IteratorProtocol, Sequence {
3133
///
3234
/// Once `nil` has been returned, all subsequent calls return `nil`.
3335
///
34-
/// - Precondition: `next()` has not been applied to a copy of `self`
35-
/// since the copy was made.
36+
/// - Returns: The next element in the underlying sequence, if a next element
37+
/// exists; otherwise, `nil`.
3638
@inlinable // FIXME(sil-serialize-all)
3739
public mutating func next() -> Element? {
3840
let result = _elements
@@ -41,7 +43,17 @@ extension IteratorOverOne: IteratorProtocol, Sequence {
4143
}
4244
}
4345

44-
/// A collection containing a single element of type `Element`.
46+
/// A collection containing a single element.
47+
///
48+
/// You can use a `CollectionOfOne` instance to efficiently represent a
49+
/// collection with only one element. For example, you can add a single element
50+
/// to an array by using a `CollectionOfOne` instance with the concatenation
51+
/// operator (`+`):
52+
///
53+
/// let a = [1, 2, 3, 4]
54+
/// let toAdd = 100
55+
/// let b = a + CollectionOfOne(toAdd)
56+
/// // b == [1, 2, 3, 4, 100]
4557
@_fixed_layout // FIXME(sil-serialize-all)
4658
public struct CollectionOfOne<Element> {
4759
@usableFromInline // FIXME(sil-serialize-all)
@@ -60,6 +72,8 @@ extension CollectionOfOne: RandomAccessCollection, MutableCollection {
6072
public typealias Indices = Range<Int>
6173

6274
/// The position of the first element.
75+
///
76+
/// In a `CollectionOfOne` instance, `startIndex` is always `0`.
6377
@inlinable // FIXME(sil-serialize-all)
6478
public var startIndex: Int {
6579
return 0
@@ -68,38 +82,45 @@ extension CollectionOfOne: RandomAccessCollection, MutableCollection {
6882
/// The "past the end" position---that is, the position one greater than the
6983
/// last valid subscript argument.
7084
///
71-
/// In a `CollectionOfOne` instance, `endIndex` is always identical to
72-
/// `index(after: startIndex)`.
85+
/// In a `CollectionOfOne` instance, `endIndex` is always `1`.
7386
@inlinable // FIXME(sil-serialize-all)
7487
public var endIndex: Int {
7588
return 1
7689
}
7790

78-
/// Always returns `endIndex`.
91+
/// Returns the position immediately after the given index.
92+
///
93+
/// - Parameter i: A valid index of the collection. `i` must be less than
94+
/// `endIndex`.
95+
/// - Returns: The index value immediately after `i`.
7996
@inlinable // FIXME(sil-serialize-all)
8097
public func index(after i: Int) -> Int {
8198
_precondition(i == startIndex)
8299
return endIndex
83100
}
84101

85-
/// Always returns `startIndex`.
102+
/// Returns the position immediately before the given index.
103+
///
104+
/// - Parameter i: A valid index of the collection. `i` must be greater than
105+
/// `startIndex`.
106+
/// - Returns: The index value immediately before `i`.
86107
@inlinable // FIXME(sil-serialize-all)
87108
public func index(before i: Int) -> Int {
88109
_precondition(i == endIndex)
89110
return startIndex
90111
}
91112

92-
/// Returns an iterator over the elements of this sequence.
113+
/// Returns an iterator over the elements of this collection.
93114
///
94-
/// - Complexity: O(1).
115+
/// - Complexity: O(1)
95116
@inlinable // FIXME(sil-serialize-all)
96117
public func makeIterator() -> IteratorOverOne<Element> {
97118
return IteratorOverOne(_elements: _element)
98119
}
99120

100121
/// Accesses the element at `position`.
101122
///
102-
/// - Precondition: `position == 0`.
123+
/// The only valid position in a `CollectionOfOne` instance is `0`.
103124
@inlinable // FIXME(sil-serialize-all)
104125
public subscript(position: Int) -> Element {
105126
get {
@@ -129,15 +150,15 @@ extension CollectionOfOne: RandomAccessCollection, MutableCollection {
129150
}
130151
}
131152

132-
/// The number of elements (always one).
153+
/// The number of elements in the collection, which is always one.
133154
@inlinable // FIXME(sil-serialize-all)
134155
public var count: Int {
135156
return 1
136157
}
137158
}
138159

139160
extension CollectionOfOne : CustomDebugStringConvertible {
140-
/// A textual representation of `self`, suitable for debugging.
161+
/// A textual representation of the collection, suitable for debugging.
141162
@inlinable // FIXME(sil-serialize-all)
142163
public var debugDescription: String {
143164
return "CollectionOfOne(\(String(reflecting: _element)))"

stdlib/public/core/CompilerProtocols.swift

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,37 +178,42 @@ public func != <T : Equatable>(lhs: T, rhs: T) -> Bool
178178
return lhs.rawValue != rhs.rawValue
179179
}
180180

181-
/// A type that can produce a collection of all of its values.
181+
/// A type that provides a collection of all of its values.
182182
///
183-
/// Simple Enumerations
184-
/// ===================
183+
/// Types that conform to the `CaseIterable` protocol are typically
184+
/// enumerations without associated values. When using a `CaseIterable` type,
185+
/// you can access a collection of all of the type's cases by using the type's
186+
/// `allCases` property.
185187
///
186-
/// For any Swift enumeration where every case does not have an associated
187-
/// value, the Swift compiler can automatically fill out the `CaseIterable`
188-
/// conformance. When defining your own custom enumeration, specify a
189-
/// conformance to `CaseIterable` to take advantage of this automatic
190-
/// derivation.
188+
/// For example, the `CompassDirection` enumeration declared in this example
189+
/// conforms to `CaseIterable`. You access the number of cases and the cases
190+
/// themselves through `CompassDirection.allCases`.
191191
///
192-
/// For example, every case of the `CardinalPoint` enumeration defined here
193-
/// does not have an associated value:
194-
///
195-
/// enum CardinalPoint: CaseIterable {
192+
/// enum CompassDirection: CaseIterable {
196193
/// case north, south, east, west
197194
/// }
198195
///
199-
/// So the compiler automatically creates a conformance.
196+
/// print("There are \(CompassDirection.allCases.count) directions.")
197+
/// // Prints "There are 4 directions."
198+
/// let caseList = CompassDirection.allCases
199+
/// .map({ "\($0)" })
200+
/// .joined(separator: ", "))
201+
/// // caseList == "north, south, east, west"
200202
///
201-
/// for cardinality in CardinalPoint.allCases {
202-
/// print(cardinality)
203-
/// }
204-
/// // Prints "north"
205-
/// // Prints "south"
206-
/// // Prints "east"
207-
/// // Prints "west"
203+
/// Conforming to the CaseIterable Protocol
204+
/// =======================================
205+
///
206+
/// The compiler can automatically provide an implementation of the
207+
/// `CaseIterable` requirements for any enumeration without associated values.
208+
/// To take advantage of this automatic synthesis when defining your own custom
209+
/// enumeration, specify conformance to the `CaseIterable` protocol in the
210+
/// original declaration.
208211
public protocol CaseIterable {
212+
/// A type that can represent a collection of all values of this type.
209213
associatedtype AllCases: Collection
210214
where AllCases.Element == Self
211-
/// Returns a collection of all values of this type.
215+
216+
/// A collection of all values of this type.
212217
static var allCases: AllCases { get }
213218
}
214219

stdlib/public/core/Range.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,29 @@ extension PartialRangeFrom: Sequence
606606
{
607607
public typealias Element = Bound
608608

609+
/// The iterator for a `PartialRangeFrom` instance.
609610
@_fixed_layout
610611
public struct Iterator: IteratorProtocol {
611612
@usableFromInline
612613
internal var _current: Bound
613614
@inlinable
614615
public init(_current: Bound) { self._current = _current }
616+
617+
/// Advances to the next element and returns it, or `nil` if no next
618+
/// element exists.
619+
///
620+
/// Once `nil` has been returned, all subsequent calls return `nil`.
621+
///
622+
/// - Returns: The next element in the underlying sequence, if a next
623+
/// element exists; otherwise, `nil`.
615624
@inlinable
616625
public mutating func next() -> Bound? {
617626
defer { _current = _current.advanced(by: 1) }
618627
return _current
619628
}
620629
}
630+
631+
/// Returns an iterator for this sequence.
621632
@inlinable
622633
public func makeIterator() -> Iterator {
623634
return Iterator(_current: lowerBound)
@@ -739,30 +750,33 @@ extension Comparable {
739750
/// unbounded range is essentially a conversion of a collection instance into
740751
/// its slice type.
741752
///
742-
/// For example, the following code declares `levenshteinDistance(_:_:)`, a
743-
/// function that calculates the number of changes required to convert one
744-
/// string into another. `levenshteinDistance(_:_:)` uses `Substring`, a
745-
/// string's slice type, for its parameters.
753+
/// For example, the following code declares `countLetterChanges(_:_:)`, a
754+
/// function that finds the number of changes required to change one
755+
/// word or phrase into another. The function uses a recursive approach to
756+
/// perform the same comparisons on smaller and smaller pieces of the original
757+
/// strings. In order to use recursion without making copies of the strings at
758+
/// each step, `countLetterChanges(_:_:)` uses `Substring`, a string's slice
759+
/// type, for its parameters.
746760
///
747-
/// func levenshteinDistance(_ s1: Substring, _ s2: Substring) -> Int {
761+
/// func countLetterChanges(_ s1: Substring, _ s2: Substring) -> Int {
748762
/// if s1.isEmpty { return s2.count }
749763
/// if s2.isEmpty { return s1.count }
750764
///
751765
/// let cost = s1.first == s2.first ? 0 : 1
752766
///
753767
/// return min(
754-
/// levenshteinDistance(s1.dropFirst(), s2) + 1,
755-
/// levenshteinDistance(s1, s2.dropFirst()) + 1,
756-
/// levenshteinDistance(s1.dropFirst(), s2.dropFirst()) + cost)
768+
/// countLetterChanges(s1.dropFirst(), s2) + 1,
769+
/// countLetterChanges(s1, s2.dropFirst()) + 1,
770+
/// countLetterChanges(s1.dropFirst(), s2.dropFirst()) + cost)
757771
/// }
758772
///
759-
/// To call `levenshteinDistance(_:_:)` with two strings, use an unbounded
760-
/// range in each string's subscript to convert it to a `Substring`.
773+
/// To call `countLetterChanges(_:_:)` with two strings, use an unbounded
774+
/// range in each string's subscript.
761775
///
762776
/// let word1 = "grizzly"
763777
/// let word2 = "grisly"
764-
/// let distance = levenshteinDistance(word1[...], word2[...])
765-
/// // distance == 2
778+
/// let changes = countLetterChanges(word1[...], word2[...])
779+
/// // changes == 2
766780
@_frozen // FIXME(sil-serialize-all)
767781
public enum UnboundedRange_ {
768782
// FIXME: replace this with a computed var named `...` when the language makes

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ extension RangeReplaceableCollection {
553553
/// Removes the elements in the specified subrange from the collection.
554554
///
555555
/// All the elements following the specified position are moved to close the
556-
/// gap. This example removes two elements from the middle of an array of
556+
/// gap. This example removes three elements from the middle of an array of
557557
/// measurements.
558558
///
559559
/// var measurements = [1.2, 1.5, 2.9, 1.2, 1.5]
@@ -754,7 +754,7 @@ extension RangeReplaceableCollection {
754754
/// Removes the elements in the specified subrange from the collection.
755755
///
756756
/// All the elements following the specified position are moved to close the
757-
/// gap. This example removes two elements from the middle of an array of
757+
/// gap. This example removes three elements from the middle of an array of
758758
/// measurements.
759759
///
760760
/// var measurements = [1.2, 1.5, 2.9, 1.2, 1.5]

0 commit comments

Comments
 (0)