Determining whether an async function will run on the main actor

As per SE-0316 (Global Actors):

A struct or class containing a wrapped instance property with a global actor-qualified wrappedValue infers actor isolation from that property wrapper:

@propertyWrapper
struct UIUpdating<Wrapped> {
  @MainActor var wrappedValue: Wrapped
}

struct CounterView { // infers @MainActor from use of @UIUpdating
  @UIUpdating var intValue: Int = 0
}

Since @StateObject has a MainActor-isolated wrapped value, a struct that contains a @StateObject property is itself inferred to be isolated to the main actor. A couple months ago I posted a pitch to disable this particular inference in Swift 6, as it seems to be quite unexpected to the community at large.

5 Likes