Skip to content

Commit 6c9f651

Browse files
committed
Clean up actor code listing.
The value returned by `endIndex` is an invalid, past-the-end index -- saving it and assuming it will become valid when adding an element isn't really good style. Explicitly removing the last element is clearer and simpler anyhow. Marking `contents` as private is a little more realistic -- that gives the actor some internal state whose concurrency is being managed.
1 parent 570c9aa commit 6c9f651

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

TSPL.docc/GuidedTour/GuidedTour.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ without waiting for it to complete,
22962296
which lets the operations run at the same time.
22972297
Because you still need to wait for the operation to finish
22982298
before you can use the value it returns,
2299-
you write `await` when you use that constant.
2299+
you write `await` when you read that constant's value.
23002300

23012301
```swift
23022302
async let cookies = makeCookies()
@@ -2322,13 +2322,11 @@ can all interact with an instance of the same actor at the same time.
23222322

23232323
```swift
23242324
actor Oven {
2325-
var contents: [String] = []
2325+
private var contents: [String] = []
23262326
func bake(_ food: String) -> String {
2327-
let index = contents.endIndex
23282327
contents.append(food)
23292328
// ... wait for food to bake ...
2330-
contents.remove(at: index)
2331-
return food
2329+
return contents.removeLast()
23322330
}
23332331
}
23342332
```
@@ -2340,11 +2338,9 @@ actor Oven {
23402338
-> actor Oven {
23412339
var contents: [String] = []
23422340
func bake(_ food: String) -> String {
2343-
let index = contents.endIndex
23442341
contents.append(food)
23452342
// ... wait for food to bake ...
2346-
contents.remove(at: index)
2347-
return food
2343+
return contents.removeLast()
23482344
}
23492345
}
23502346
```
@@ -2358,9 +2354,6 @@ that's already running on the actor to finish.
23582354
```swift
23592355
let oven = Oven()
23602356
let biscuits = await oven.bake("biscuits")
2361-
for item in await oven.contents {
2362-
print(item)
2363-
}
23642357
```
23652358

23662359
<!--
@@ -2369,9 +2362,6 @@ for item in await oven.contents {
23692362
```swifttest
23702363
-> let oven = Oven()
23712364
-> let biscuits = await oven.bake("biscuits")
2372-
-> for item in await oven.contents {
2373-
print(item)
2374-
}
23752365
```
23762366
-->
23772367

0 commit comments

Comments
 (0)