# Example of Actors (Concurrency) errors out

**URL:** <https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662>\
**Category:** Site Feedback\
**Tags:** swift-book\
**Created:** [November 23, 2021, 1:17pm UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662 "2021-11-23T13:17:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NSMau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsmau/32/29230_2.png) [@NSMau](https://forums.swift.org/u/NSMau)\
**Post date:** [November 23, 2021, 1:17pm UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/1 "2021-11-23T13:17:35Z")

</div>

The example shown for the `Actors` section of the [Concurrency](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html) chapter of the book, errors out when running it as-is.

Here's the code for the example:

> [...] For example, here’s an actor that records temperatures:

```swift
actor TemperatureLogger {
    let label: String
    var measurements: [Int]
    private(set) var max: Int

    init(label: String, measurement: Int) {
        self.label = label
        self.measurements = [measurement]
        self.max = measurement
    }
}

```

> ...  
> You create an instance of an actor using the same initializer syntax as structures and classes. When you access a property or method of an actor, you use `await` to mark the potential suspension point—for example:

```swift
let logger = TemperatureLogger(label: "Outdoors", measurement: 25)
print(await logger.max)
// Prints "25"

```

However, when running this code via the `swift` command in the terminal, it errors out as shown below:

 ![Screen Shot 2021-11-23 at 8.12.12 AM](https://global.discourse-cdn.com/swift/original/3X/3/c/3ccbca2c4f2344cd1cf968eec8e83f6bc86f7ef3.png)

I managed, however, to make it work—by refactoring the code to access the `max` property of the actor as follows:

```swift
Task {
    let logger = TemperatureLogger(label: "Outdoors", measurement: 25)
    print(await logger.max)
}

```

In such case, it prints `25`, as expected.

---

<div class="post-metadata">

**Author:** ![Joseph\_Heck](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joseph_heck/32/22369_2.png) [@Joseph\_Heck](https://forums.swift.org/u/Joseph_Heck)\
**Post date:** [November 24, 2021, 1:09am UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/2 "2021-11-24T01:09:15Z")

</div>

/cc @Alex_Martini

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [November 24, 2021, 1:45am UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/3 "2021-11-24T01:45:30Z")

</div>

The snippet is "right" but only partial I suppose...

The general rule about any `await` is that it must be inside of an asynchronous closure or function. `Task{}` creates such closure, but you could also write it as

```swift
func x() async { 
    let logger = TemperatureLogger(label: "Outdoors", measurement: 25)
    print(await logger.max)
}

```

I'm not sure about adjusting the example and have to iterate all the cases where you'd be able to write `await`.

* * *

Above in the document it is explained where you can use `await`:

> Because code with `await` needs to be able to suspend execution, only certain places in your program can call asynchronous functions or methods:
> 
> - Code in the body of an asynchronous function, method, or property.
> - Code in the static `main()` method of a structure, class, or enumeration that’s marked with `@main` .
> - Code in a detached child task, as shown in [Unstructured Concurrency](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html#ID643) below.

So that intends to cover this.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [November 24, 2021, 2:20am UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/4 "2021-11-24T02:20:52Z")

</div>

Though that sentence has a bug anyway, we'll fix it (a detached task is not a child task)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 24, 2021, 4:06am UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/5 "2021-11-24T04:06:26Z")

</div>

> [@ktoso](#):
>
> So that intends to cover this.

I'd agree with @NSMau that it's a documentation bug that the code as-is gives an error. There is no indication anywhere near the example code that it needs to be encapsulated "in" anything else and users should not need to parse the whole document to figure out the unstated rules that will make an example actually compile.

On a related note, though—this is the second time in just a week or two that a user has been unable to figure out how to use concurrency features because top-level code doesn't behave like code in a `@main` type's `main()`. In this case, even the reference text elides that difference.

I recall asking in one of the earlier concurrency pitches specifically about this issue (although I can't seem to find that thread any more) where top-level code and code inside a `@main` type's `main()` behave differently. I thought it would be a progressive disclosure problem—which is now borne out with the questions on these forums. I remember being satisfied (and again, I can't seem to find that thread anymore) that the intended design was for top-level code to behave no differently than using a `@main` type's `main()`—how come that's not the case anymore with concurrency features, and can we fix it?

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [November 24, 2021, 4:11am UTC](https://forums.swift.org/t/example-of-actors-concurrency-errors-out/53662/6 "2021-11-24T04:11:15Z")

</div>

I don’t disagree, but neither can I do more here than to try to get it fixed (I reached out and hope to get it done). It would be nice if the docs were an open-source page we can PR to, but sadly that isn’t the case.

Re the general issue: there was/is a hope to allow top level code to have async code in it, but that again has not been done — if that were implemented it would have solved the issue here.

I wish I could do more here, but we’ll have to go through the process of updating the book the usual route, and it’s up to the editors to decide if they want to nest everything in `Task{}` or not… Alex is among the right people to ping here, so things happen after thanksgiving here.
