@@ -190,27 +190,23 @@ public protocol RawRepresentable {
190
190
var rawValue : RawValue { get }
191
191
}
192
192
193
- /// Returns a Boolean value indicating whether the two operands are equal.
193
+ /// Returns a Boolean value indicating whether the two arguments are equal.
194
194
///
195
195
/// - Parameters:
196
196
/// - lhs: A raw-representable instance.
197
197
/// - rhs: A second raw-representable instance.
198
- /// - Returns: `true` if the two operands have equal raw values; otherwise,
199
- /// `false`.
200
198
@warn_unused_result
201
199
public func == <
202
200
T : RawRepresentable where T. RawValue : Equatable
203
201
> ( lhs: T , rhs: T ) -> Bool {
204
202
return lhs. rawValue == rhs. rawValue
205
203
}
206
204
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.
208
206
///
209
207
/// - Parameters:
210
208
/// - lhs: A raw-representable instance.
211
209
/// - rhs: A second raw-representable instance.
212
- /// - Returns: `true` if the two operands have unequal raw values; otherwise,
213
- /// `false`.
214
210
@warn_unused_result
215
211
public func != <
216
212
T : RawRepresentable where T. RawValue : Equatable
@@ -220,13 +216,11 @@ public func != <
220
216
221
217
// This overload is needed for ambiguity resolution against the
222
218
// 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.
224
220
///
225
221
/// - Parameters:
226
222
/// - lhs: A raw-representable instance.
227
223
/// - rhs: A second raw-representable instance.
228
- /// - Returns: `true` if the two operands have unequal raw values; otherwise,
229
- /// `false`.
230
224
@warn_unused_result
231
225
public func != <
232
226
T : Equatable where T : RawRepresentable , T. RawValue : Equatable
@@ -478,9 +472,69 @@ public protocol ArrayLiteralConvertible {
478
472
init ( arrayLiteral elements: Element ... )
479
473
}
480
474
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
+ /// }
482
534
public protocol DictionaryLiteralConvertible {
535
+ /// The key type of a dictionary literal.
483
536
associatedtype Key
537
+ /// The value type of a dictionary literal.
484
538
associatedtype Value
485
539
/// Create an instance initialized with `elements`.
486
540
init ( dictionaryLiteral elements: ( Key , Value ) ... )
0 commit comments