Skip to content

Commit 642cbba

Browse files
committed
[stdlib] Documentation revisions and expansions
- Adding docs for unbounded ranges - Filling in missing docs for range-expression subscripts - Equality operators for arrays - Fix issues with range discussions - Fill in missing integer docs
1 parent 8b96071 commit 642cbba

File tree

8 files changed

+303
-94
lines changed

8 files changed

+303
-94
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,20 @@ public struct ${Self}<Element>: _DestructorSafeContainer {
491491
}
492492

493493
extension ${Self}: RandomAccessCollection, MutableCollection {
494+
/// The index type for arrays, `Int`.
495+
%if Self == 'ArraySlice':
496+
///
497+
/// `ArraySlice` instances are not always indexed from zero. Use `startIndex`
498+
/// and `endIndex` as the bounds for any element access, instead of `0` and
499+
/// `count`.
500+
%end
494501
public typealias Index = Int
502+
503+
/// The type that represents the indices that are valid for subscripting an
504+
/// array, in ascending order.
495505
public typealias Indices = Range<Int>
506+
507+
/// The type that allows iteration over an array's elements.
496508
public typealias Iterator = IndexingIterator<${Self}>
497509

498510
%if Self == 'ArraySlice':
@@ -541,6 +553,11 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
541553
%end
542554
}
543555

556+
/// Returns the position immediately after the given index.
557+
///
558+
/// - Parameter i: A valid index of the collection. `i` must be less than
559+
/// `endIndex`.
560+
/// - Returns: The index immediately after `i`.
544561
@_inlineable
545562
public func index(after i: Int) -> Int {
546563
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -551,6 +568,10 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
551568
return i + 1
552569
}
553570

571+
/// Replaces the given index with its successor.
572+
///
573+
/// - Parameter i: A valid index of the collection. `i` must be less than
574+
/// `endIndex`.
554575
@_inlineable
555576
public func formIndex(after i: inout Int) {
556577
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -561,6 +582,11 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
561582
i += 1
562583
}
563584

585+
/// Returns the position immediately before the given index.
586+
///
587+
/// - Parameter i: A valid index of the collection. `i` must be greater than
588+
/// `startIndex`.
589+
/// - Returns: The index immediately before `i`.
564590
@_inlineable
565591
public func index(before i: Int) -> Int {
566592
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -571,6 +597,10 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
571597
return i - 1
572598
}
573599

600+
/// Replaces the given index with its predecessor.
601+
///
602+
/// - Parameter i: A valid index of the collection. `i` must be greater than
603+
/// `startIndex`.
574604
@_inlineable
575605
public func formIndex(before i: inout Int) {
576606
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -2200,7 +2230,15 @@ extension _ArrayBufferProtocol {
22002230
% for (Self, a_Self) in arrayTypes:
22012231

22022232
extension ${Self} : Equatable where Element : Equatable {
2203-
/// Returns `true` if these arrays contain the same elements.
2233+
/// Returns a Boolean value indicating whether two arrays contain the same
2234+
/// elements in the same order.
2235+
///
2236+
/// You can use the equal-to operator (`==`) to compare any two arrays
2237+
/// that store the same, `Equatable`-conforming element type.
2238+
///
2239+
/// - Parameters:
2240+
/// - lhs: An array to compare.
2241+
/// - rhs: Another array to compare.
22042242
@_inlineable
22052243
public static func ==(lhs: ${Self}<Element>, rhs: ${Self}<Element>) -> Bool {
22062244
let lhsCount = lhs.count
@@ -2243,7 +2281,15 @@ extension ${Self} : Equatable where Element : Equatable {
22432281
return true
22442282
}
22452283

2246-
/// Returns `true` if the arrays do not contain the same elements.
2284+
/// Returns a Boolean value indicating whether two arrays are not equal.
2285+
///
2286+
/// Two arrays are equal if they contain the same elements in the same order.
2287+
/// You can use the not-equal-to operator (`!=`) to compare any two arrays
2288+
/// that store the same, `Equatable`-conforming element type.
2289+
///
2290+
/// - Parameters:
2291+
/// - lhs: An array to compare.
2292+
/// - rhs: Another array to compare.
22472293
@_inlineable
22482294
public static func !=(lhs: ${Self}<Element>, rhs: ${Self}<Element>) -> Bool {
22492295
return !(lhs == rhs)

stdlib/public/core/ClosedRange.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// FIXME: swift-3-indexing-model: Generalize all tests to check both
1414
// [Closed]Range.
1515

16-
/// A closed range that forms a collection of consecutive values.
16+
/// An interval from a lower bound up to, and including, an upper bound.
1717
///
1818
/// You create a `ClosedRange` instance by using the closed range
1919
/// operator (`...`).
@@ -23,51 +23,49 @@
2323
/// A `ClosedRange` instance contains both its lower bound and its
2424
/// upper bound.
2525
///
26-
/// print(throughFive.contains(3)) // Prints "true"
27-
/// print(throughFive.contains(10)) // Prints "false"
28-
/// print(throughFive.contains(5)) // Prints "true"
26+
/// throughFive.contains(3)
27+
/// // true
28+
/// throughFive.contains(10)
29+
/// // false
30+
/// throughFive.contains(5)
31+
/// // true
2932
///
3033
/// Because a closed range includes its upper bound, a closed range whose lower
31-
/// bound is equal to the upper bound contains one element. Therefore, a
34+
/// bound is equal to the upper bound contains that value. Therefore, a
3235
/// `ClosedRange` instance cannot represent an empty range.
3336
///
3437
/// let zeroInclusive = 0...0
35-
/// print(zeroInclusive.isEmpty)
36-
/// // Prints "false"
37-
/// print(zeroInclusive.count)
38-
/// // Prints "1"
38+
/// zeroInclusive.contains(0)
39+
/// // true
40+
/// zeroInclusive.isEmpty
41+
/// // false
3942
///
40-
/// You can use a `for`-`in` loop or any sequence or collection method with a
41-
/// countable range. The elements of the range are the consecutive values from
42-
/// its lower bound up to, and including, its upper bound.
43+
/// Using a Closed Range as a Collection of Consecutive Values
44+
/// ----------------------------------------------------------
4345
///
44-
/// for n in throughFive.suffix(3) {
46+
/// When a closed range uses integers as its lower and upper bounds, or any
47+
/// other type that conforms to the `Strideable` protocol with an integer
48+
/// stride, you can use that range in a `for`-`in` loop or with any sequence or
49+
/// collection method. The elements of the range are the consecutive values
50+
/// from its lower bound up to, and including, its upper bound.
51+
///
52+
/// for n in 3...5 {
4553
/// print(n)
4654
/// }
4755
/// // Prints "3"
4856
/// // Prints "4"
4957
/// // Prints "5"
5058
///
51-
/// You can create a countable range over any type that conforms to the
52-
/// `Strideable` protocol and uses an integer as its associated `Stride` type.
53-
/// By default, Swift's integer and pointer types are usable as the bounds of
54-
/// a countable range.
55-
///
5659
/// Because floating-point types such as `Float` and `Double` are their own
5760
/// `Stride` types, they cannot be used as the bounds of a countable range. If
58-
/// you need to test whether values are contained within a closed interval
59-
/// bound by floating-point values, see the `ClosedRange` type. If you need to
60-
/// iterate over consecutive floating-point values, see the
61+
/// you need to iterate over consecutive floating-point values, see the
6162
/// `stride(from:through:by:)` function.
6263
@_fixed_layout
6364
public struct ClosedRange<Bound: Comparable> {
6465
/// The range's lower bound.
6566
public let lowerBound: Bound
6667

6768
/// The range's upper bound.
68-
///
69-
/// `upperBound` is always reachable from `lowerBound` by zero or
70-
/// more applications of `index(after:)`.
7169
public let upperBound: Bound
7270

7371
/// Creates an instance with the given bounds.

stdlib/public/core/Collection.swift

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public typealias Indexable = Collection
6969
/// }
7070
/// }
7171
///
72-
/// The `CollectionOfTwo` type uses the default iterator type,
73-
/// `IndexingIterator`, because it doesn't define its own `makeIterator()`
74-
/// method or `Iterator` associated type. This example shows how a
75-
/// `CollectionOfTwo` instance can be created holding the values of a point,
76-
/// and then iterated over using a `for`-`in` loop.
72+
/// Because `CollectionOfTwo` doesn't define its own `makeIterator()`
73+
/// method or `Iterator` associated type, it uses the default iterator type,
74+
/// `IndexingIterator`. This example shows how a `CollectionOfTwo` instance
75+
/// can be created holding the values of a point, and then iterated over
76+
/// using a `for`-`in` loop.
7777
///
7878
/// let point = CollectionOfTwo(15.0, 20.0)
7979
/// for element in point {
@@ -426,22 +426,35 @@ public protocol Collection: Sequence where SubSequence: Collection {
426426

427427
/// Accesses a contiguous subrange of the collection's elements.
428428
///
429-
/// The accessed slice uses the same indices for the same elements as the
430-
/// original collection uses. Always use the slice's `startIndex` property
431-
/// instead of assuming that its indices start at a particular value.
432-
///
433-
/// This example demonstrates getting a slice of an array of strings, finding
434-
/// the index of one of the strings in the slice, and then using that index
435-
/// in the original array.
429+
/// For example, using a `PartialRangeFrom` range expression with an array
430+
/// accesses the subrange from the start of the range expression until the
431+
/// end of the array.
436432
///
437433
/// let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
438-
/// let streetsSlice = streets[2 ..< streets.endIndex]
434+
/// let streetsSlice = streets[2..<5]
439435
/// print(streetsSlice)
440-
/// // Prints "["Channing", "Douglas", "Evarts"]"
436+
/// // ["Channing", "Douglas", "Evarts"]
441437
///
442-
/// let index = streetsSlice.index(of: "Evarts") // 4
443-
/// print(streets[index!])
444-
/// // Prints "Evarts"
438+
/// The accessed slice uses the same indices for the same elements as the
439+
/// original collection. This example searches `streetsSlice` for one of the
440+
/// strings in the slice, and then uses that index in the original array.
441+
///
442+
/// let index = streetsSlice.index(of: "Evarts")! // 4
443+
/// print(streets[index])
444+
/// // "Evarts"
445+
///
446+
/// Always use the slice's `startIndex` property instead of assuming that its
447+
/// indices start at a particular value. Attempting to access an element by
448+
/// using an index outside the bounds of the slice may result in a runtime
449+
/// error, even if that index is valid for the original collection.
450+
///
451+
/// print(streetsSlice.startIndex)
452+
/// // 2
453+
/// print(streetsSlice[2])
454+
/// // "Channing"
455+
///
456+
/// print(streetsSlice[0])
457+
/// // error: Index out of bounds
445458
///
446459
/// - Parameter bounds: A range of the collection's indices. The bounds of
447460
/// the range must be valid indices of the collection.
@@ -1044,7 +1057,7 @@ extension Collection where SubSequence == Slice<Self> {
10441057
/// Accesses a contiguous subrange of the collection's elements.
10451058
///
10461059
/// The accessed slice uses the same indices for the same elements as the
1047-
/// original collection uses. Always use the slice's `startIndex` property
1060+
/// original collection. Always use the slice's `startIndex` property
10481061
/// instead of assuming that its indices start at a particular value.
10491062
///
10501063
/// This example demonstrates getting a slice of an array of strings, finding

stdlib/public/core/Integers.swift.gyb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,12 +1585,18 @@ ${assignmentOperatorComment(x.nonMaskingOperator, False)}
15851585
}
15861586

15871587
extension BinaryInteger {
1588+
/// Creates a new value equal to zero.
15881589
@_inlineable // FIXME(sil-serialize-all)
15891590
@_transparent
15901591
public init() {
15911592
self = 0
15921593
}
15931594
1595+
/// Returns `-1` if this value is negative and `1` if it's positive;
1596+
/// otherwise, `0`.
1597+
///
1598+
/// - Returns: The sign of this number, expressed as an integer of the same
1599+
/// type.
15941600
@_inlineable // FIXME(sil-serialize-all)
15951601
@_transparent
15961602
public func signum() -> Self {
@@ -1835,6 +1841,7 @@ extension Int {
18351841
return other - self
18361842
}
18371843

1844+
// FIXME(ABI): using Int as the parameter type is wrong.
18381845
/// Returns a value that is offset the specified distance from this value.
18391846
///
18401847
/// Use the `advanced(by:)` method in generic code to offset a value by a
@@ -1846,7 +1853,6 @@ extension Int {
18461853
///
18471854
/// - Parameter n: The distance to advance this value.
18481855
/// - Returns: A value that is offset from this value by `n`.
1849-
// FIXME(ABI): using Int as the parameter type is wrong.
18501856
@_inlineable // FIXME(sil-serialize-all)
18511857
@_transparent
18521858
public func advanced(by n: Int) -> Int {
@@ -3000,6 +3006,7 @@ public struct ${Self}
30003006
: FixedWidthInteger, ${Unsigned}Integer,
30013007
_ExpressibleByBuiltinIntegerLiteral {
30023008

3009+
/// A type that represents an integer literal.
30033010
public typealias IntegerLiteralType = ${Self}
30043011

30053012

0 commit comments

Comments
 (0)