How do Tasks inherit context?

From the Apple Docs for Task.init(priority:operation:):

Runs the given nonthrowing operation asynchronously as part of a new top-level task on behalf of the current actor.

I was wondering how the initialiser figures out the "current actor". Does anyone know what magic is happening inside the implementation of the initialiser to figure out what the current actor is when Task.init(priority:operation:) is called? What if a function isn't running on any actor and you call Task.init(priority:operation:), how does it figure it out then?

I think it uses @_inheritActorContext to annotate the operation closure similar to View.task in SwiftUI.

1 Like

Thank you so much for the two links. They're both super super informative

1 Like