Skip to content

Commit 7b64ca5

Browse files
committed
Update prose for the new delegate example.
The discussion of how the delegate can access the type that's doing the delegation is no longer relevant -- DiceGameTracker doesn't access any game state, and there's no longer a need to define a protocol for the delegating type to conform to. So that explanation was removed without replacement.
1 parent a5bb677 commit 7b64ca5

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

TSPL.docc/LanguageGuide/Protocols.md

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ code using existential types incurs pointer indirection and dynamic method dispa
808808
that cannot be optimized away.
809809
-->
810810

811-
812811
## Delegation
813812

814813
*Delegation* is a design pattern that enables
@@ -893,15 +892,16 @@ is marked by its inheritance from `AnyObject`,
893892
as discussed in <doc:Protocols#Class-Only-Protocols>.
894893

895894
`DiceGame.Delegate` provides three methods for tracking the progress of a game.
896-
These three methods have been incorporated into the game logic within
897-
the `play()` method above, and are called when
895+
These three methods are incorporated into the game logic
896+
in the `play(rounds:)` method above.
897+
The `DiceGame` class calls its delegate methods when
898898
a new game starts, a new turn begins, or the game ends.
899899

900900
Because the `delegate` property is an *optional* `DiceGame.Delegate`,
901-
the `play()` method uses optional chaining each time it calls a method on the delegate,
901+
the `play(rounds:)` method uses optional chaining each time it calls a method on the delegate,
902902
as discussed in <doc:OptionalChaining>.
903903
If the `delegate` property is nil,
904-
these delegate calls fail gracefully and without error.
904+
these delegate calls are just skipped.
905905
If the `delegate` property is non-nil,
906906
the delegate methods are called,
907907
and are passed the `DiceGame` instance as a parameter.
@@ -941,33 +941,14 @@ class DiceGameTracker: DiceGame.Delegate {
941941
}
942942
```
943943

944-
<!-- XXX rewrite paragraph -->
945-
`DiceGameTracker` implements all three methods required by `DiceGame.Delegate`.
946-
It uses these methods to keep track of the number of turns a game has taken.
947-
It resets a `numberOfTurns` property to zero when the game starts,
948-
increments it each time a new turn begins,
949-
and prints out the total number of turns once the game has ended.
950-
951-
<!-- XXX rewrite paragraph -->
952-
The implementation of `gameDidStart(_:)` shown above uses the `game` parameter
953-
to print some introductory information about the game that's about to be played.
954-
The `game` parameter has a type of `DiceGame`, not `SnakesAndLadders`,
955-
and so `gameDidStart(_:)` can access and use only methods and properties that
956-
are implemented as part of the `DiceGame` protocol.
957-
However, the method is still able to use type casting to
958-
query the type of the underlying instance.
959-
In this example, it checks whether `game` is actually
960-
an instance of `SnakesAndLadders` behind the scenes,
961-
and prints an appropriate message if so.
962-
963-
<!-- XXX rewrite paragraph -->
964-
The `gameDidStart(_:)` method also accesses the `dice` property of the passed `game` parameter.
965-
Because `game` is known to conform to the `DiceGame` protocol,
966-
it's guaranteed to have a `dice` property,
967-
and so the `gameDidStart(_:)` method is able to access and print the dice's `sides` property,
968-
regardless of what kind of game is being played.
969-
970-
Here's how `DiceGameTracker` looks in action:
944+
The `DiceGameTracker` class implements all three methods
945+
that are required by the `DiceGame.Delegate` protocol.
946+
It uses these methods to zero out both players' scores
947+
at the start of a new game,
948+
to update their scores at the end of each round,
949+
and to announce a winner at the end of the game.
950+
951+
Here's how `DiceGame` and `DiceGameTracker` look in action:
971952

972953
```swift
973954
let tracker = DiceGameTracker()

0 commit comments

Comments
 (0)