Skip to content

Commit 653a4d8

Browse files
committed
[stdlib] Revise documentation comments
- Clarify RawValue requirement for OptionSet - Make Bool counterexample more clearly an error - Add clarifying note about unsafe arithmetic methods - Add/refine complexity docs for sequence/collection - Remove docs from obsoleted symbols - Standardize on unicode.org for links about Unicode - More complexity annotations - Expand dictionary defaulted subscript docs - Fix error in Dictionary.init(minimumCapacity:) docs - Improve accuracy of prime number listing
1 parent 14ea94a commit 653a4d8

File tree

7 files changed

+75
-45
lines changed

7 files changed

+75
-45
lines changed

stdlib/public/core/Bool.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
/// print(i)
4444
/// i -= 1
4545
/// }
46+
/// // error: 'Int' is not convertible to 'Bool'
4647
///
4748
/// The correct approach in Swift is to compare the `i` value with zero in the
4849
/// `while` statement.

stdlib/public/core/Dictionary.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,15 @@ import SwiftShims
294294
/// dictionary called `interestingNumbers` with string keys and values that
295295
/// are integer arrays, then sorts each array in-place in descending order.
296296
///
297-
/// var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 15],
297+
/// var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 17],
298298
/// "triangular": [1, 3, 6, 10, 15, 21, 28],
299299
/// "hexagonal": [1, 6, 15, 28, 45, 66, 91]]
300300
/// for key in interestingNumbers.keys {
301301
/// interestingNumbers[key]?.sort(by: >)
302302
/// }
303303
///
304304
/// print(interestingNumbers["primes"]!)
305-
/// // Prints "[15, 13, 11, 7, 5, 3, 2]"
305+
/// // Prints "[17, 13, 11, 7, 5, 3, 2]"
306306
///
307307
/// Iterating Over the Contents of a Dictionary
308308
/// ===========================================
@@ -408,7 +408,7 @@ public struct Dictionary<Key: Hashable, Value> {
408408
///
409409
/// - Parameter minimumCapacity: The minimum number of key-value pairs that
410410
/// the newly created dictionary should be able to store without
411-
// reallocating its storage buffer.
411+
/// reallocating its storage buffer.
412412
@inlinable // FIXME(sil-serialize-all)
413413
public init(minimumCapacity: Int) {
414414
_variantBuffer = .native(_NativeBuffer(minimumCapacity: minimumCapacity))
@@ -805,8 +805,56 @@ extension Dictionary: ExpressibleByDictionaryLiteral {
805805
}
806806

807807
extension Dictionary {
808-
/// Accesses the element with the given key, or the specified default value,
809-
/// if the dictionary doesn't contain the given key.
808+
/// Accesses the value with the given key. If the dictionary doesn't contain
809+
/// the given key, accesses the provided default value as if the key and
810+
/// default value existed in the dictionary.
811+
///
812+
/// Use this subscript when you want either the value for a particular key
813+
/// or, when that key is not present in the dictionary, a default value. This
814+
/// example uses the subscript with a message to use in case an HTTP response
815+
/// code isn't recognized:
816+
///
817+
/// var responseMessages = [200: "OK",
818+
/// 403: "Access forbidden",
819+
/// 404: "File not found",
820+
/// 500: "Internal server error"]
821+
///
822+
/// let httpResponseCodes = [200, 403, 301]
823+
/// for code in httpResponseCodes {
824+
/// let message = responseMessages[code, default: "Unknown response"]
825+
/// print("Response \(code): \(message)")
826+
/// }
827+
/// // Prints "Response 200: OK"
828+
/// // Prints "Response 403: Access Forbidden"
829+
/// // Prints "Response 301: Unknown response"
830+
///
831+
/// When a dictionary's `Value` type has value semantics, you can use this
832+
/// subscript to perform in-place operations on values in the dictionary.
833+
/// The following example uses this subscript while counting the occurences
834+
/// of each letter in a string:
835+
///
836+
/// let message = "Hello, Elle!"
837+
/// var letterCounts: [Character: Int] = [:]
838+
/// for letter in message {
839+
/// letterCounts[letter, defaultValue: 0] += 1
840+
/// }
841+
/// // letterCounts == ["H": 1, "e": 2, "l": 4, "o": 1, ...]
842+
///
843+
/// When `letterCounts[letter, defaultValue: 0] += 1` is executed with a
844+
/// value of `letter` that isn't already a key in `letterCounts`, the
845+
/// specified default value (`0`) is returned from the subscript,
846+
/// incremented, and then added to the dictionary under that key.
847+
///
848+
/// - Note: Do not use this subscript to modify dictionary values if the
849+
/// dictionary's `Value` type is a class. In that case, the default value
850+
/// and key are not written back to the dictionary after an operation.
851+
///
852+
/// - Parameters:
853+
/// - key: The key the look up in the dictionary.
854+
/// - defaultValue: The default value to use if `key` doesn't exist in the
855+
/// dictionary.
856+
/// - Returns: The value associated with `key` in the dictionary`; otherwise,
857+
/// `defaultValue`.
810858
@inlinable // FIXME(sil-serialize-all)
811859
public subscript(
812860
key: Key, default defaultValue: @autoclosure () -> Value

stdlib/public/core/Equatable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
/// example shows how the `contains(_:)` method can be used with an array of
3030
/// strings.
3131
///
32-
/// let students = ["Nora", "Fern", "Ryan", "Rainer"]
32+
/// let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
3333
///
34-
/// let nameToCheck = "Ryan"
34+
/// let nameToCheck = "Kofi"
3535
/// if students.contains(nameToCheck) {
3636
/// print("\(nameToCheck) is signed up!")
3737
/// } else {
3838
/// print("No record of \(nameToCheck).")
3939
/// }
40-
/// // Prints "Ryan is signed up!"
40+
/// // Prints "Kofi is signed up!"
4141
///
4242
/// Conforming to the Equatable Protocol
4343
/// ====================================

stdlib/public/core/InputStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SwiftShims
1919
/// Unicode [replacement characters][rc].
2020
///
2121
/// [rc]:
22-
/// http://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
22+
/// https://unicode.org/glossary/#replacement_character
2323
///
2424
/// - Parameter strippingNewline: If `true`, newline characters and character
2525
/// combinations are stripped from the result; otherwise, newline characters

stdlib/public/core/Integers.swift.gyb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,10 @@ def unsafeOperationComment(operator):
977977
/// In debug builds (`-Onone`) a runtime error is still triggered if the
978978
/// operation overflows.
979979
///
980+
/// This method is not a synonym for the masking addition operator (`&+`).
981+
/// Use that operator instead of this method when you want to discard any
982+
/// overflow that results from an addition operation.
983+
///
980984
/// - Parameter rhs: The value to add to this value.
981985
/// - Returns: The sum of this value and `rhs`.
982986
""",
@@ -993,6 +997,10 @@ def unsafeOperationComment(operator):
993997
/// In debug builds (`-Onone`) a runtime error is still triggered if the
994998
/// operation overflows.
995999
///
1000+
/// This method is not a synonym for the masking subtraction operator (`&-`).
1001+
/// Use that operator instead of this method when you want to discard any
1002+
/// overflow that results from a subtraction operation.
1003+
///
9961004
/// - Parameter rhs: The value to subtract from this value.
9971005
/// - Returns: The result of subtracting `rhs` from this value.
9981006
""",
@@ -1009,6 +1017,10 @@ def unsafeOperationComment(operator):
10091017
/// In debug builds (`-Onone`) a runtime error is still triggered if the
10101018
/// operation overflows.
10111019
///
1020+
/// This method is not a synonym for the masking multiplication operator
1021+
/// (`&*`). Use that operator instead of this method when you want to discard
1022+
/// any overflow that results from an addition operation.
1023+
///
10121024
/// - Parameter rhs: The value to multiply by this value.
10131025
/// - Returns: The product of this value and `rhs`.
10141026
""",
@@ -2417,22 +2429,6 @@ ${assignmentOperatorComment(x.operator, False)}
24172429

24182430
extension FixedWidthInteger {
24192431
/// The empty bitset.
2420-
///
2421-
/// The `allZeros` static property is the [identity element][] for bitwise OR
2422-
/// and XOR operations and the [fixed point][] for bitwise AND operations.
2423-
/// For example:
2424-
///
2425-
/// let x: UInt8 = 5 // 0b00000101
2426-
///
2427-
/// // Identity
2428-
/// x | .allZeros // 0b00000101
2429-
/// x ^ .allZeros // 0b00000101
2430-
///
2431-
/// // Fixed point
2432-
/// x & .allZeros // 0b00000000
2433-
///
2434-
/// [identity element]:http://en.wikipedia.org/wiki/Identity_element
2435-
/// [fixed point]:http://en.wikipedia.org/wiki/Fixed_point_(mathematics)
24362432
@inlinable // FIXME(sil-serialize-all)
24372433
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0")
24382434
public static var allZeros: Self { return 0 }

stdlib/public/core/OptionSet.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
/// requires no extra work on your part.
2121
///
2222
/// When creating an option set, include a `rawValue` property in your type
23-
/// declaration. The `rawValue` property must be of a type that conforms to
24-
/// the `FixedWidthInteger` protocol, such as `Int` or `UInt8`. Next, create
25-
/// unique options as static properties of your custom type using unique
26-
/// powers of two (1, 2, 4, 8, 16, and so forth) for each individual
23+
/// declaration. For your type to automatically receive default implementations
24+
/// for set-related operations, the `rawValue` property must be of a type that
25+
/// conforms to the `FixedWidthInteger` protocol, such as `Int` or `UInt8`.
26+
/// Next, create unique options as static properties of your custom type using
27+
/// unique powers of two (1, 2, 4, 8, 16, and so forth) for each individual
2728
/// property's raw value so that each property can be represented by a single
2829
/// bit of the type's raw value.
2930
///

stdlib/public/core/Policy.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -454,22 +454,6 @@ public protocol _BitwiseOperations {
454454
static prefix func ~ (x: Self) -> Self
455455

456456
/// The empty bitset.
457-
///
458-
/// The `allZeros` static property is the [identity element][] for bitwise OR
459-
/// and XOR operations and the [fixed point][] for bitwise AND operations.
460-
/// For example:
461-
///
462-
/// let x: UInt8 = 5 // 0b00000101
463-
///
464-
/// // Identity
465-
/// x | .allZeros // 0b00000101
466-
/// x ^ .allZeros // 0b00000101
467-
///
468-
/// // Fixed point
469-
/// x & .allZeros // 0b00000000
470-
///
471-
/// [identity element]:http://en.wikipedia.org/wiki/Identity_element
472-
/// [fixed point]:http://en.wikipedia.org/wiki/Fixed_point_(mathematics)
473457
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0 or init() of a type conforming to FixedWidthInteger")
474458
static var allZeros: Self { get }
475459
}

0 commit comments

Comments
 (0)