Skip to content

[stdlib] Revise documentation comments #17109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/// print(i)
/// i -= 1
/// }
/// // error: 'Int' is not convertible to 'Bool'
///
/// The correct approach in Swift is to compare the `i` value with zero in the
/// `while` statement.
Expand Down
58 changes: 53 additions & 5 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@ import SwiftShims
/// dictionary called `interestingNumbers` with string keys and values that
/// are integer arrays, then sorts each array in-place in descending order.
///
/// var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 15],
/// var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 17],
/// "triangular": [1, 3, 6, 10, 15, 21, 28],
/// "hexagonal": [1, 6, 15, 28, 45, 66, 91]]
/// for key in interestingNumbers.keys {
/// interestingNumbers[key]?.sort(by: >)
/// }
///
/// print(interestingNumbers["primes"]!)
/// // Prints "[15, 13, 11, 7, 5, 3, 2]"
/// // Prints "[17, 13, 11, 7, 5, 3, 2]"
///
/// Iterating Over the Contents of a Dictionary
/// ===========================================
Expand Down Expand Up @@ -408,7 +408,7 @@ public struct Dictionary<Key: Hashable, Value> {
///
/// - Parameter minimumCapacity: The minimum number of key-value pairs that
/// the newly created dictionary should be able to store without
// reallocating its storage buffer.
/// reallocating its storage buffer.
@inlinable // FIXME(sil-serialize-all)
public init(minimumCapacity: Int) {
_variantBuffer = .native(_NativeBuffer(minimumCapacity: minimumCapacity))
Expand Down Expand Up @@ -805,8 +805,56 @@ extension Dictionary: ExpressibleByDictionaryLiteral {
}

extension Dictionary {
/// Accesses the element with the given key, or the specified default value,
/// if the dictionary doesn't contain the given key.
/// Accesses the value with the given key. If the dictionary doesn't contain
/// the given key, accesses the provided default value as if the key and
/// default value existed in the dictionary.
///
/// Use this subscript when you want either the value for a particular key
/// or, when that key is not present in the dictionary, a default value. This
/// example uses the subscript with a message to use in case an HTTP response
/// code isn't recognized:
///
/// var responseMessages = [200: "OK",
/// 403: "Access forbidden",
/// 404: "File not found",
/// 500: "Internal server error"]
///
/// let httpResponseCodes = [200, 403, 301]
/// for code in httpResponseCodes {
/// let message = responseMessages[code, default: "Unknown response"]
/// print("Response \(code): \(message)")
/// }
/// // Prints "Response 200: OK"
/// // Prints "Response 403: Access Forbidden"
/// // Prints "Response 301: Unknown response"
///
/// When a dictionary's `Value` type has value semantics, you can use this
/// subscript to perform in-place operations on values in the dictionary.
/// The following example uses this subscript while counting the occurences
/// of each letter in a string:
///
/// let message = "Hello, Elle!"
/// var letterCounts: [Character: Int] = [:]
/// for letter in message {
/// letterCounts[letter, defaultValue: 0] += 1
/// }
/// // letterCounts == ["H": 1, "e": 2, "l": 4, "o": 1, ...]
///
/// When `letterCounts[letter, defaultValue: 0] += 1` is executed with a
/// value of `letter` that isn't already a key in `letterCounts`, the
/// specified default value (`0`) is returned from the subscript,
/// incremented, and then added to the dictionary under that key.
///
/// - Note: Do not use this subscript to modify dictionary values if the
/// dictionary's `Value` type is a class. In that case, the default value
/// and key are not written back to the dictionary after an operation.
///
/// - Parameters:
/// - key: The key the look up in the dictionary.
/// - defaultValue: The default value to use if `key` doesn't exist in the
/// dictionary.
/// - Returns: The value associated with `key` in the dictionary`; otherwise,
/// `defaultValue`.
@inlinable // FIXME(sil-serialize-all)
public subscript(
key: Key, default defaultValue: @autoclosure () -> Value
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
/// example shows how the `contains(_:)` method can be used with an array of
/// strings.
///
/// let students = ["Nora", "Fern", "Ryan", "Rainer"]
/// let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
///
/// let nameToCheck = "Ryan"
/// let nameToCheck = "Kofi"
/// if students.contains(nameToCheck) {
/// print("\(nameToCheck) is signed up!")
/// } else {
/// print("No record of \(nameToCheck).")
/// }
/// // Prints "Ryan is signed up!"
/// // Prints "Kofi is signed up!"
///
/// Conforming to the Equatable Protocol
/// ====================================
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/InputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SwiftShims
/// Unicode [replacement characters][rc].
///
/// [rc]:
/// http://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
/// https://unicode.org/glossary/#replacement_character
///
/// - Parameter strippingNewline: If `true`, newline characters and character
/// combinations are stripped from the result; otherwise, newline characters
Expand Down
28 changes: 12 additions & 16 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ def unsafeOperationComment(operator):
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking addition operator (`&+`).
/// Use that operator instead of this method when you want to discard any
/// overflow that results from an addition operation.
///
/// - Parameter rhs: The value to add to this value.
/// - Returns: The sum of this value and `rhs`.
""",
Expand All @@ -993,6 +997,10 @@ def unsafeOperationComment(operator):
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking subtraction operator (`&-`).
/// Use that operator instead of this method when you want to discard any
/// overflow that results from a subtraction operation.
///
/// - Parameter rhs: The value to subtract from this value.
/// - Returns: The result of subtracting `rhs` from this value.
""",
Expand All @@ -1009,6 +1017,10 @@ def unsafeOperationComment(operator):
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking multiplication operator
/// (`&*`). Use that operator instead of this method when you want to discard
/// any overflow that results from an addition operation.
///
/// - Parameter rhs: The value to multiply by this value.
/// - Returns: The product of this value and `rhs`.
""",
Expand Down Expand Up @@ -2417,22 +2429,6 @@ ${assignmentOperatorComment(x.operator, False)}

extension FixedWidthInteger {
/// The empty bitset.
///
/// The `allZeros` static property is the [identity element][] for bitwise OR
/// and XOR operations and the [fixed point][] for bitwise AND operations.
/// For example:
///
/// let x: UInt8 = 5 // 0b00000101
///
/// // Identity
/// x | .allZeros // 0b00000101
/// x ^ .allZeros // 0b00000101
///
/// // Fixed point
/// x & .allZeros // 0b00000000
///
/// [identity element]:http://en.wikipedia.org/wiki/Identity_element
/// [fixed point]:http://en.wikipedia.org/wiki/Fixed_point_(mathematics)
@inlinable // FIXME(sil-serialize-all)
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0")
public static var allZeros: Self { return 0 }
Expand Down
9 changes: 5 additions & 4 deletions stdlib/public/core/OptionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
/// requires no extra work on your part.
///
/// When creating an option set, include a `rawValue` property in your type
/// declaration. The `rawValue` property must be of a type that conforms to
/// the `FixedWidthInteger` protocol, such as `Int` or `UInt8`. Next, create
/// unique options as static properties of your custom type using unique
/// powers of two (1, 2, 4, 8, 16, and so forth) for each individual
/// declaration. For your type to automatically receive default implementations
/// for set-related operations, the `rawValue` property must be of a type that
/// conforms to the `FixedWidthInteger` protocol, such as `Int` or `UInt8`.
/// Next, create unique options as static properties of your custom type using
/// unique powers of two (1, 2, 4, 8, 16, and so forth) for each individual
/// property's raw value so that each property can be represented by a single
/// bit of the type's raw value.
///
Expand Down
16 changes: 0 additions & 16 deletions stdlib/public/core/Policy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,6 @@ public protocol _BitwiseOperations {
static prefix func ~ (x: Self) -> Self

/// The empty bitset.
///
/// The `allZeros` static property is the [identity element][] for bitwise OR
/// and XOR operations and the [fixed point][] for bitwise AND operations.
/// For example:
///
/// let x: UInt8 = 5 // 0b00000101
///
/// // Identity
/// x | .allZeros // 0b00000101
/// x ^ .allZeros // 0b00000101
///
/// // Fixed point
/// x & .allZeros // 0b00000000
///
/// [identity element]:http://en.wikipedia.org/wiki/Identity_element
/// [fixed point]:http://en.wikipedia.org/wiki/Fixed_point_(mathematics)
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0 or init() of a type conforming to FixedWidthInteger")
static var allZeros: Self { get }
}
Expand Down