Skip to content

Commit 311e1dc

Browse files
committed
[stdlib] Add documentation for key-path appending methods
1 parent 781f632 commit 311e1dc

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,24 +1354,112 @@ func _projectKeyPathReferenceWritable<Root, Value>(
13541354
public protocol _AppendKeyPath {}
13551355

13561356
extension _AppendKeyPath where Self == AnyKeyPath {
1357+
/// Returns a new key path created by appending the given key path to this
1358+
/// one.
1359+
///
1360+
/// Use this method to extend this key path to the value type of another key
1361+
/// path. Appending the key path passed as `path` is successful only if the
1362+
/// root type for `path` matches this key path's value type. This example
1363+
/// creates key paths from `Array<Int>` to `String` and from `String` to
1364+
/// `Int`, and then tries appending each to the other:
1365+
///
1366+
/// let arrayDescription: AnyKeyPath = \Array<Int>.description
1367+
/// let stringLength: AnyKeyPath = \String.count
1368+
///
1369+
/// // Creates a key path from `Array<Int>` to `Int`
1370+
/// let arrayDescriptionLength = arrayDescription.appending(path: stringLength)
1371+
///
1372+
/// let invalidKeyPath = stringLength.appending(path: arrayDescription)
1373+
/// // invalidKeyPath == nil
1374+
///
1375+
/// The second call to `appending(path:)` returns `nil`
1376+
/// because the root type of `arrayDescription`, `Array<Int>`, does not
1377+
/// match the value type of `stringLength`, `Int`.
1378+
///
1379+
/// - Parameter path: The key path to append.
1380+
/// - Returns: A key path from the root of this key path and the value type
1381+
/// of `path`, if `path` can be appended. If `path` can't be appended,
1382+
/// returns `nil`.
13571383
public func appending(path: AnyKeyPath) -> AnyKeyPath? {
13581384
return _tryToAppendKeyPaths(root: self, leaf: path)
13591385
}
13601386
}
13611387

13621388
extension _AppendKeyPath /* where Self == PartialKeyPath<T> */ {
1389+
/// Returns a new key path created by appending the given key path to this
1390+
/// one.
1391+
///
1392+
/// Use this method to extend this key path to the value type of another key
1393+
/// path. Appending the key path passed as `path` is successful only if the
1394+
/// root type for `path` matches this key path's value type. This example
1395+
/// creates key paths from `Array<Int>` to `String` and from `String` to
1396+
/// `Int`, and then tries appending each to the other:
1397+
///
1398+
/// let arrayDescription: PartialKeyPath<Array<Int>> = \.description
1399+
/// let stringLength: PartialKeyPath<String> = \.count
1400+
///
1401+
/// // Creates a key path from `Array<Int>` to `Int`
1402+
/// let arrayDescriptionLength = arrayDescription.appending(path: stringLength)
1403+
///
1404+
/// let invalidKeyPath = stringLength.appending(path: arrayDescription)
1405+
/// // invalidKeyPath == nil
1406+
///
1407+
/// The second call to `appending(path:)` returns `nil`
1408+
/// because the root type of `arrayDescription`, `Array<Int>`, does not
1409+
/// match the value type of `stringLength`, `Int`.
1410+
///
1411+
/// - Parameter path: The key path to append.
1412+
/// - Returns: A key path from the root of this key path and the value type
1413+
/// of `path`, if `path` can be appended. If `path` can't be appended,
1414+
/// returns `nil`.
13631415
public func appending<Root>(path: AnyKeyPath) -> PartialKeyPath<Root>?
13641416
where Self == PartialKeyPath<Root> {
13651417
return _tryToAppendKeyPaths(root: self, leaf: path)
13661418
}
13671419

1420+
/// Returns a new key path created by appending the given key path to this
1421+
/// one.
1422+
///
1423+
/// Use this method to extend this key path to the value type of another key
1424+
/// path. Appending the key path passed as `path` is successful only if the
1425+
/// root type for `path` matches this key path's value type. This example
1426+
/// creates a key path from `Array<Int>` to `String`, and then tries
1427+
/// appending compatible and incompatible key paths:
1428+
///
1429+
/// let arrayDescription: PartialKeyPath<Array<Int>> = \.description
1430+
///
1431+
/// // Creates a key path from `Array<Int>` to `Int`
1432+
/// let arrayDescriptionLength = arrayDescription.appending(path: \String.count)
1433+
///
1434+
/// let invalidKeyPath = arrayDescription.appending(path: \Double.isZero)
1435+
/// // invalidKeyPath == nil
1436+
///
1437+
/// The second call to `appending(path:)` returns `nil` because the root type
1438+
/// of the `path` parameter, `Double`, does not match the value type of
1439+
/// `arrayDescription`, `String`.
1440+
///
1441+
/// - Parameter path: The key path to append.
1442+
/// - Returns: A key path from the root of this key path to the the value type
1443+
/// of `path`, if `path` can be appended. If `path` can't be appended,
1444+
/// returns `nil`.
13681445
public func appending<Root, AppendedRoot, AppendedValue>(
13691446
path: KeyPath<AppendedRoot, AppendedValue>
13701447
) -> KeyPath<Root, AppendedValue>?
13711448
where Self == PartialKeyPath<Root> {
13721449
return _tryToAppendKeyPaths(root: self, leaf: path)
13731450
}
13741451

1452+
/// Returns a new key path created by appending the given key path to this
1453+
/// one.
1454+
///
1455+
/// Use this method to extend this key path to the value type of another key
1456+
/// path. Appending the key path passed as `path` is successful only if the
1457+
/// root type for `path` matches this key path's value type.
1458+
///
1459+
/// - Parameter path: The reference writeable key path to append.
1460+
/// - Returns: A key path from the root of this key path to the the value type
1461+
/// of `path`, if `path` can be appended. If `path` can't be appended,
1462+
/// returns `nil`.
13751463
public func appending<Root, AppendedRoot, AppendedValue>(
13761464
path: ReferenceWritableKeyPath<AppendedRoot, AppendedValue>
13771465
) -> ReferenceWritableKeyPath<Root, AppendedValue>?
@@ -1381,6 +1469,22 @@ extension _AppendKeyPath /* where Self == PartialKeyPath<T> */ {
13811469
}
13821470

13831471
extension _AppendKeyPath /* where Self == KeyPath<T,U> */ {
1472+
/// Returns a new key path created by appending the given key path to this
1473+
/// one.
1474+
///
1475+
/// Use this method to extend this key path to the value type of another key
1476+
/// path. Calling `appending(path:)` results in the same key path as if the
1477+
/// given key path had been specified using dot notation. In the following
1478+
/// example, `keyPath1` and `keyPath2` are equivalent:
1479+
///
1480+
/// let arrayDescription = \Array<Int>.description
1481+
/// let keyPath1 = arrayDescription.appending(path: \String.count)
1482+
///
1483+
/// let keyPath2 = \Array<Int>.description.count
1484+
///
1485+
/// - Parameter path: The key path to append.
1486+
/// - Returns: A key path from the root of this key path to the value type of
1487+
/// `path`.
13841488
public func appending<Root, Value, AppendedValue>(
13851489
path: KeyPath<Value, AppendedValue>
13861490
) -> KeyPath<Root, AppendedValue>
@@ -1399,6 +1503,16 @@ extension _AppendKeyPath /* where Self == KeyPath<T,U> */ {
13991503
}
14001504
*/
14011505

1506+
/// Returns a new key path created by appending the given key path to this
1507+
/// one.
1508+
///
1509+
/// Use this method to extend this key path to the value type of another key
1510+
/// path. Calling `appending(path:)` results in the same key path as if the
1511+
/// given key path had been specified using dot notation.
1512+
///
1513+
/// - Parameter path: The key path to append.
1514+
/// - Returns: A key path from the root of this key path to the value type of
1515+
/// `path`.
14021516
public func appending<Root, Value, AppendedValue>(
14031517
path: ReferenceWritableKeyPath<Value, AppendedValue>
14041518
) -> ReferenceWritableKeyPath<Root, AppendedValue>
@@ -1408,13 +1522,33 @@ extension _AppendKeyPath /* where Self == KeyPath<T,U> */ {
14081522
}
14091523

14101524
extension _AppendKeyPath /* where Self == WritableKeyPath<T,U> */ {
1525+
/// Returns a new key path created by appending the given key path to this
1526+
/// one.
1527+
///
1528+
/// Use this method to extend this key path to the value type of another key
1529+
/// path. Calling `appending(path:)` results in the same key path as if the
1530+
/// given key path had been specified using dot notation.
1531+
///
1532+
/// - Parameter path: The key path to append.
1533+
/// - Returns: A key path from the root of this key path to the value type of
1534+
/// `path`.
14111535
public func appending<Root, Value, AppendedValue>(
14121536
path: WritableKeyPath<Value, AppendedValue>
14131537
) -> WritableKeyPath<Root, AppendedValue>
14141538
where Self == WritableKeyPath<Root, Value> {
14151539
return _appendingKeyPaths(root: self, leaf: path)
14161540
}
14171541

1542+
/// Returns a new key path created by appending the given key path to this
1543+
/// one.
1544+
///
1545+
/// Use this method to extend this key path to the value type of another key
1546+
/// path. Calling `appending(path:)` results in the same key path as if the
1547+
/// given key path had been specified using dot notation.
1548+
///
1549+
/// - Parameter path: The key path to append.
1550+
/// - Returns: A key path from the root of this key path to the value type of
1551+
/// `path`.
14181552
public func appending<Root, Value, AppendedValue>(
14191553
path: ReferenceWritableKeyPath<Value, AppendedValue>
14201554
) -> ReferenceWritableKeyPath<Root, AppendedValue>
@@ -1424,6 +1558,16 @@ extension _AppendKeyPath /* where Self == WritableKeyPath<T,U> */ {
14241558
}
14251559

14261560
extension _AppendKeyPath /* where Self == ReferenceWritableKeyPath<T,U> */ {
1561+
/// Returns a new key path created by appending the given key path to this
1562+
/// one.
1563+
///
1564+
/// Use this method to extend this key path to the value type of another key
1565+
/// path. Calling `appending(path:)` results in the same key path as if the
1566+
/// given key path had been specified using dot notation.
1567+
///
1568+
/// - Parameter path: The key path to append.
1569+
/// - Returns: A key path from the root of this key path to the value type of
1570+
/// `path`.
14271571
public func appending<Root, Value, AppendedValue>(
14281572
path: WritableKeyPath<Value, AppendedValue>
14291573
) -> ReferenceWritableKeyPath<Root, AppendedValue>

0 commit comments

Comments
 (0)