Skip to content

Commit b7432d8

Browse files
committed
[stdlib] Revisions to dictionaries and 'Policy' protocols
This revises documentation for Dictionary and the related types and protocols, including Equatable, Comparable, and Hashable.
1 parent e26bd64 commit b7432d8

File tree

5 files changed

+1205
-169
lines changed

5 files changed

+1205
-169
lines changed

stdlib/public/core/CompilerProtocols.swift

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,23 @@ public protocol RawRepresentable {
190190
var rawValue: RawValue { get }
191191
}
192192

193-
/// Returns a Boolean value indicating whether the two operands are equal.
193+
/// Returns a Boolean value indicating whether the two arguments are equal.
194194
///
195195
/// - Parameters:
196196
/// - lhs: A raw-representable instance.
197197
/// - rhs: A second raw-representable instance.
198-
/// - Returns: `true` if the two operands have equal raw values; otherwise,
199-
/// `false`.
200198
@warn_unused_result
201199
public func == <
202200
T : RawRepresentable where T.RawValue : Equatable
203201
>(lhs: T, rhs: T) -> Bool {
204202
return lhs.rawValue == rhs.rawValue
205203
}
206204

207-
/// Returns a Boolean value indicating whether the two operands are not equal.
205+
/// Returns a Boolean value indicating whether the two arguments are not equal.
208206
///
209207
/// - Parameters:
210208
/// - lhs: A raw-representable instance.
211209
/// - rhs: A second raw-representable instance.
212-
/// - Returns: `true` if the two operands have unequal raw values; otherwise,
213-
/// `false`.
214210
@warn_unused_result
215211
public func != <
216212
T : RawRepresentable where T.RawValue : Equatable
@@ -220,13 +216,11 @@ public func != <
220216

221217
// This overload is needed for ambiguity resolution against the
222218
// implementation of != for T : Equatable
223-
/// Returns a Boolean value indicating whether the two operands are not equal.
219+
/// Returns a Boolean value indicating whether the two arguments are not equal.
224220
///
225221
/// - Parameters:
226222
/// - lhs: A raw-representable instance.
227223
/// - rhs: A second raw-representable instance.
228-
/// - Returns: `true` if the two operands have unequal raw values; otherwise,
229-
/// `false`.
230224
@warn_unused_result
231225
public func != <
232226
T : Equatable where T : RawRepresentable, T.RawValue : Equatable
@@ -478,9 +472,69 @@ public protocol ArrayLiteralConvertible {
478472
init(arrayLiteral elements: Element...)
479473
}
480474

481-
/// Conforming types can be initialized with dictionary literals.
475+
/// A type that can be initialized using a dictionary literal.
476+
///
477+
/// A dictionary literal is a simple way of writing a list of key-value pairs.
478+
/// You write each key-value pair with a colon (`:`) separating the key and
479+
/// the value. The dictionary literal is made up of one or more key-value
480+
/// pairs, separated by commas and surrounded with square brackets.
481+
///
482+
/// To declare a dictionary, assign a dictionary literal to a variable or
483+
/// constant:
484+
///
485+
/// let countryCodes = ["BR": "Brazil", "GH": "Ghana",
486+
/// "JP": "Japan", "US": "United States"]
487+
/// // 'countryCodes' has type [String: String]
488+
///
489+
/// print(countryCodes["BR"]!)
490+
/// // Prints "Brazil"
491+
///
492+
/// When the context provides enough type information, you can use a special
493+
/// form of the dictionary literal, square brackets surrounding a single
494+
/// colon, to initialize an empty dictionary.
495+
///
496+
/// var frequencies: [String: Int] = [:]
497+
/// print(frequencies.count)
498+
/// // Prints "0"
499+
///
500+
/// - Note: A dictionary literal is *not* the same as an instance of
501+
/// `Dictionary` or the similarly named `DictionaryLiteral` type. You can't
502+
/// initialize a type that conforms to `DictionaryLiteralConvertible` simply
503+
/// by assigning an instance of one of these types.
504+
///
505+
/// Conforming to the DictionaryLiteralConvertible Protocol
506+
/// =======================================================
507+
///
508+
/// To add the capability to be initialized with a dictionary literal to your
509+
/// own custom types, declare an `init(dictionaryLiteral:)` initializer. The
510+
/// following example shows the dictionary literal initializer for a
511+
/// hypothetical `CountedSet` type, which uses setlike semantics while keeping
512+
/// track of the count for duplicate elements:
513+
///
514+
/// struct CountedSet<Element: Hashable>: Collection, SetAlgebra {
515+
/// // implementation details
516+
///
517+
/// /// Updates the count stored in the set for the given element,
518+
/// /// adding the element if necessary.
519+
/// ///
520+
/// /// - Parameter n: The new count for `element`. `n` must be greater
521+
/// /// than or equal to zero.
522+
/// /// - Parameter element: The element to set the new count on.
523+
/// mutating func updateCount(_ n: Int, for element: Element)
524+
/// }
525+
///
526+
/// extension CountedSet: DictionaryLiteralConvertible {
527+
/// init(dictionaryLiteral elements: (Element, Int)...) {
528+
/// self.init()
529+
/// for (element, count) in elements {
530+
/// self.updateCount(count, for: element)
531+
/// }
532+
/// }
533+
/// }
482534
public protocol DictionaryLiteralConvertible {
535+
/// The key type of a dictionary literal.
483536
associatedtype Key
537+
/// The value type of a dictionary literal.
484538
associatedtype Value
485539
/// Create an instance initialized with `elements`.
486540
init(dictionaryLiteral elements: (Key, Value)...)

stdlib/public/core/FixedPoint.swift.gyb

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,16 +546,27 @@ public func ${op}(lhs: ${Self}, rhs: ${Self}) -> ${Self} {
546546
%end
547547

548548
// Bitwise negate
549+
/// Returns the inverse of the bits set in the argument.
550+
///
551+
/// - SeeAlso: `BitwiseOperations`
549552
@_transparent
550553
public prefix func ~(rhs: ${Self}) -> ${Self} {
551554
let mask = ${Self}.subtractWithOverflow(0, 1).0
552555
return ${Self}(Builtin.xor_${BuiltinName}(rhs._value, mask._value))
553556
}
554557

555-
% for op, name in (
556-
% ('==','eq'), ('!=','ne'),
557-
% ('<', sign + 'lt'), ('<=', sign + 'le'),
558-
% ('>', sign + 'gt'), ('>=', sign + 'ge')):
558+
% for op, name, comment in (
559+
% ('==', 'eq', 'the two arguments have equal values'),
560+
% ('!=', 'ne', 'the two arguments have unequal values'),
561+
% ('<', sign + 'lt', 'the first argument is less than the second argument'),
562+
% ('<=', sign + 'le', 'the first argument is less than or equal to the second argument'),
563+
% ('>', sign + 'gt', 'the first argument is greater than the second argument'),
564+
% ('>=', sign + 'ge', 'the first argument is greater than or equal to the second argument'),
565+
% ):
566+
/// Returns a Boolean value that indicates whether
567+
/// ${comment}.
568+
///
569+
/// - SeeAlso: `Equatable`, `Comparable`
559570
@_transparent
560571
public func ${op} (lhs: ${Self}, rhs: ${Self}) -> Bool {
561572
return Bool(Builtin.cmp_${name}_${BuiltinName}(lhs._value, rhs._value))
@@ -576,22 +587,38 @@ public func ${op} (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
576587
}
577588
% end
578589

579-
% for op, name in (('&','and'), ('^','xor'), ('|','or')):
590+
% for op, name, comment in (
591+
% ('&', 'and', 'intersection of bits set in'),
592+
% ('^', 'xor', 'bits that are set in exactly one of'),
593+
% ('|', 'or', 'union of bits set in'),
594+
% ):
595+
/// Returns the ${comment} the two arguments.
596+
///
597+
/// - SeeAlso: `BitwiseOperations`
580598
@_transparent
581599
public func ${op} (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
582600
return ${Self}(Builtin.${name}_${BuiltinName}(lhs._value, rhs._value))
583601
}
602+
603+
/// Calculates the ${comment} the two arguments
604+
/// and stores the result in the first argument.
605+
///
606+
/// - SeeAlso: `BitwiseOperations`
607+
@_transparent
608+
public func ${op}=(lhs: inout ${Self}, rhs: ${Self}) {
609+
lhs = lhs ${op} rhs
610+
}
584611
% end
585612

586613
// Bitwise operations
587614
@_transparent
588615
extension ${Self} : BitwiseOperations {
589-
/// The empty bitset of type ${Self}.
616+
/// The empty bitset of type `${Self}`.
590617
public static var allZeros: ${Self} { return 0 }
591618
}
592619

593620
// Compound assignments
594-
% for op in '+', '-', '*', '<<', '>>', '&', '|', '^':
621+
% for op in '+', '-', '*', '<<', '>>':
595622
@_transparent
596623
public func ${op}=(lhs: inout ${Self}, rhs: ${Self}) {
597624
lhs = lhs ${op} rhs

0 commit comments

Comments
 (0)