struct Box {
      let action: () -> Void
  }
  
  @MainActor
  func f() -> Void {}
  
  let b = Box(action: f)
  b.action()

It doesn't seem legit to me. as action isn't an async or MainActor clsoure.

That's behaviour introduced by swift-evolution/proposals/0434-global-actor-isolated-types-usability.md at main · swiftlang/swift-evolution · GitHub

I read the proposal. It doesn’t seem like the same thing.

I know the reason. thanks.

Yeah, I forgot detail that top-level code implicitly run on main actor. That's why that's legal. If you wrap that in a nonisolated function, it will raise an error:

nonisolated func run() {
    let b = Box(action: f)
    b.action()
}

But in Swift 5 it actually warns about loosing main actor isolation, while in Swift 6 the top-level version don't, because of the inference.

1 Like