Calling escaping closure from actor context: Why don't I need `await` here?

Suppose I am using some, pre-Swift 5.5, API that looks like this:

func whoKnowsWhatQueueThisWillExecuteOn(closure: @escaping () -> ())

Now, let's suppose I set up an actor like this:

actor Foo {
    var bar: String = "bar"
    func setBar(_ bar: String) { = bar }

    func baz() {
        whoKnowsWhatQueueThisWillExecuteOn {
            // also compiles without the `Task`
            Task {
                self.setBar("baz") // why don't I need 'await' here?

This compiles fine, but I don't quite understand why. Shouldn't I need an await here? Since the closure is escaping, there's no way for the compiler to know what context it will be called from, what queue it will be called on, or even when it will be called.

Am I missing something?


Bump. I'm still curious about this...

1 Like
Terms of Service

Privacy Policy

Cookie Policy