Skip to content

Commit d951ee0

Browse files
committed
Remove duplicated content.
1 parent 67553db commit d951ee0

File tree

1 file changed

+69
-152
lines changed

1 file changed

+69
-152
lines changed

TSPL.docc/GuidedTour/GuidedTour.md

Lines changed: 69 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,75 @@ Task {
18421842
```
18431843
-->
18441844

1845+
Use task groups and tasks to structure concurrent code.
1846+
Writing `async`-`let` implicitly creates a task.
1847+
1848+
```swift
1849+
let userIDs = await withTaskGroup(of: Int.self) { taskGroup in
1850+
for server in ["primary", "secondary", "development"] {
1851+
taskGroup.addTask {
1852+
return await fetchUserID(from: server)
1853+
}
1854+
}
1855+
1856+
var results: [Int] = []
1857+
for await result in taskGroup {
1858+
results.append(result)
1859+
}
1860+
return results
1861+
}
1862+
```
1863+
1864+
Actors are similar to classes,
1865+
except they ensure that different asynchronous functions
1866+
can all interact with an instance of the same actor at the same time.
1867+
1868+
```swift
1869+
actor Oven {
1870+
private var contents: [String] = []
1871+
func bake(_ food: String) -> String {
1872+
contents.append(food)
1873+
// ... wait for food to bake ...
1874+
return contents.removeLast()
1875+
}
1876+
}
1877+
```
1878+
1879+
<!--
1880+
- test: `guided-tour`
1881+
1882+
```swifttest
1883+
-> actor Oven {
1884+
var contents: [String] = []
1885+
func bake(_ food: String) -> String {
1886+
contents.append(food)
1887+
// ... wait for food to bake ...
1888+
return contents.removeLast()
1889+
}
1890+
}
1891+
```
1892+
-->
1893+
1894+
When you call a method on an actor or access one of its properties,
1895+
you mark that code with `await`
1896+
to indicate that it might have to wait for other code
1897+
that's already running on the actor to finish.
1898+
1899+
```swift
1900+
let oven = Oven()
1901+
let biscuits = await oven.bake("biscuits")
1902+
```
1903+
1904+
<!--
1905+
- test: `guided-tour`
1906+
1907+
```swifttest
1908+
-> let oven = Oven()
1909+
-> let biscuits = await oven.bake("biscuits")
1910+
```
1911+
-->
1912+
1913+
18451914
## Protocols and Extensions
18461915

18471916
Use `protocol` to declare a protocol.
@@ -2257,158 +2326,6 @@ print(fridgeIsOpen)
22572326
```
22582327
-->
22592328

2260-
## Concurrency
2261-
2262-
You use `async` to mark an asynchronous function.
2263-
You mark an asynchronous operation,
2264-
like a call to an asynchronous function,
2265-
by writing `await`
2266-
if you want to wait for it to complete.
2267-
2268-
```swift
2269-
func bake(_ food: String) async -> String {
2270-
// ... wait for food to bake ...
2271-
return food
2272-
}
2273-
func makeCookies() async -> String {
2274-
let cookies = await bake("cookies")
2275-
return cookies
2276-
}
2277-
```
2278-
2279-
<!--
2280-
- test: `guided-tour`
2281-
2282-
```swifttest
2283-
-> func bake(_ food: String) async -> String {
2284-
// ... wait for food to bake ...
2285-
return food
2286-
}
2287-
-> func makeCookies() async -> String {
2288-
let cookies = await bake("cookies")
2289-
return cookies
2290-
}
2291-
```
2292-
-->
2293-
2294-
You can use `async`-`let` to start an asynchronous operation
2295-
without waiting for it to complete,
2296-
which lets the operations run at the same time.
2297-
Because you still need to wait for the operation to finish
2298-
before you can use the value it returns,
2299-
you write `await` when you read that constant's value.
2300-
2301-
```swift
2302-
async let cookies = makeCookies()
2303-
async let bread = bake("bread")
2304-
2305-
let bakedGoods = await [cookies, bread]
2306-
```
2307-
2308-
<!--
2309-
- test: guided-tour-async
2310-
2311-
```swifttest
2312-
-> async let cookies = makeCookies()
2313-
-> async let bread = bake("bread")
2314-
---
2315-
-> let bakedGoods = await [cookies, bread]
2316-
```
2317-
-->
2318-
2319-
You structure concurrent code using task groups and child tasks.
2320-
Writing `async`-`let` implicitly creates a child task.
2321-
2322-
```swift
2323-
let desserts = await withTaskGroup(of: String.self) { group in
2324-
for recipe in ["biscuit", "croissant", "egg tart"] {
2325-
group.addTask {
2326-
return await bake(recipe)
2327-
}
2328-
}
2329-
2330-
var results: [String] = []
2331-
for await result in group {
2332-
results.append(result)
2333-
}
2334-
2335-
return results
2336-
}
2337-
```
2338-
2339-
2340-
<!--
2341-
- test: guided-tour-async
2342-
2343-
``` swifttest
2344-
-> let desserts = await withTaskGroup(of: String.self) { group in
2345-
for recipe in ["biscuit", "croissant", "egg tart"] {
2346-
group.addTask {
2347-
return await bake(recipe)
2348-
}
2349-
}
2350-
---
2351-
var results: [String] = []
2352-
for await result in group {
2353-
results.append(result)
2354-
}
2355-
---
2356-
return results
2357-
}
2358-
>> print(desserts)
2359-
<< ["biscuit", "croissant", "egg tart"]
2360-
```
2361-
-->
2362-
2363-
Actors are similar to classes,
2364-
except they ensure that different asynchronous functions
2365-
can all interact with an instance of the same actor at the same time.
2366-
2367-
```swift
2368-
actor Oven {
2369-
private var contents: [String] = []
2370-
func bake(_ food: String) -> String {
2371-
contents.append(food)
2372-
// ... wait for food to bake ...
2373-
return contents.removeLast()
2374-
}
2375-
}
2376-
```
2377-
2378-
<!--
2379-
- test: `guided-tour`
2380-
2381-
```swifttest
2382-
-> actor Oven {
2383-
var contents: [String] = []
2384-
func bake(_ food: String) -> String {
2385-
contents.append(food)
2386-
// ... wait for food to bake ...
2387-
return contents.removeLast()
2388-
}
2389-
}
2390-
```
2391-
-->
2392-
2393-
When you call a method on an actor or access one of its properties,
2394-
you mark that code with `await`
2395-
to indicate that it might have to wait for other code
2396-
that's already running on the actor to finish.
2397-
2398-
```swift
2399-
let oven = Oven()
2400-
let biscuits = await oven.bake("biscuits")
2401-
```
2402-
2403-
<!--
2404-
- test: `guided-tour`
2405-
2406-
```swifttest
2407-
-> let oven = Oven()
2408-
-> let biscuits = await oven.bake("biscuits")
2409-
```
2410-
-->
2411-
24122329
## Generics
24132330

24142331
Write a name inside angle brackets

0 commit comments

Comments
 (0)