Skip to content

Commit beca5b3

Browse files
committed
Update the example
1 parent 35c07e9 commit beca5b3

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

proposals/NNNN-global-actor-isolated-types-usability.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,18 @@ However, there is nothing unsafe about treating `x` as nonisolated. The general
3333

3434
We can do better than that, though. It should be possible to treat a `var` stored property of a global-actor value type as *implicitly* `nonisolated` under the same conditions that a `let` property can be. A stored property from a different module can be changed to a computed property in the future, and those future computed accessors may need to be isolated to the global actor, so allowing access across module boundaries would not be okay for source or binary compatibility. But within the module that defines the property, we know that hasn't happened, so it's fine to use a more relaxed rule.
3535

36-
Next, under the current concurrency rules, it is possible for a function type to be both isolated to a global actor and yet not required to be `Sendable`. This is not a useful combination: such a function can only be used if the current context is isolated to the global actor, and in that case the global actor annotation is unnecessary because *all* non-`Sendable` functions will run with global actor isolation. It would be better for a global actor attribute to always imply `@Sendable`:
36+
Next, under the current concurrency rules, it is possible for a function type to be both isolated to a global actor and yet not required to be `Sendable`. This is not a useful combination: such a function can only be used if the current context is isolated to the global actor, and in that case the global actor annotation is unnecessary because *all* non-`Sendable` functions will run with global actor isolation.
3737

3838
```swift
39-
func test() {
40-
let closure: @MainActor () -> Void = {
41-
print("hmmmm")
42-
}
43-
39+
func test(globallyIsolated: @escaping @MainActor () -> Void) {
4440
Task {
45-
// error: capture of 'closure' with non-sendable type '@MainActor () -> Void' in a `@Sendable` closure
46-
await closure()
41+
// error: capture of 'globallyIsolated' with non-sendable type '@MainActor () -> Void' in a `@Sendable` closure
42+
await globallyIsolated()
4743
}
4844
}
4945
```
5046

47+
It would be better for a global actor attribute to always imply `@Sendable`.
5148

5249
Because a globally-isolated closure cannot be called concurrently, it's safe for it to capture non-`Sendable` values even if it's implicitly `@Sendable`. Such values just need to be transferred to the global actor's region (if they aren't there already). The same logic also applies to closures that are isolated to a specific actor reference, although it isn't currently possible to write such a closure in a context that isn't isolated to that actor.
5350

@@ -134,18 +131,14 @@ The programmer can still choose to explicitly mark a stored property `nonisolate
134131
To improve usability of globally-isolated functions and closures, under this proposal `@Sendable` is inferred:
135132

136133
```swift
137-
func test() {
138-
let closure: @MainActor () -> Void = {
139-
print("hmmmm")
140-
}
141-
134+
func test(globallyIsolated: @escaping @MainActor () -> Void) {
142135
Task {
143-
await closure() // okay
136+
await globallyIsolated() //okay
144137
}
145138
}
146139
```
147140

148-
The closure in the above code is global-actor isolated via the `@MainActor`. Thus, it can never operate on the same reference concurrently at the same time, making it safe to be invoked from different isolation domains. This means that for such global-actor-isolated closures and functions, the `@Sendable` attribute is implicit.
141+
The `globallyIsolated` closure in the above code is global-actor isolated via the `@MainActor`. Thus, it can never operate on the same reference concurrently at the same time, making it safe to be invoked from different isolation domains. This means that for such global-actor-isolated closures and functions, the `@Sendable` attribute is implicit.
149142

150143
#### Non-`Sendable` captures in isolated closures
151144

0 commit comments

Comments
 (0)