carlhung
(Carlhung)
1
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.
vns
2
carlhung
(Carlhung)
3
I read the proposal. It doesn’t seem like the same thing.
carlhung
(Carlhung)
4
I know the reason. thanks.
vns
5
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