Skip to content

Instantly share code, notes, and snippets.

@NikolaiRuhe
Created July 8, 2026 07:22
Show Gist options
  • Select an option

  • Save NikolaiRuhe/eedc554de9b1cb1861b0385b8683b3c8 to your computer and use it in GitHub Desktop.

Select an option

Save NikolaiRuhe/eedc554de9b1cb1861b0385b8683b3c8 to your computer and use it in GitHub Desktop.
Illustrate the fact that nonsending asynchronous functions know their isolation.
import Testing
@Test func tests() async {
await nonisolatedFunc()
await mainActorIsolatedFunc()
await SomeActor().someActorIsolatedFunc()
}
@concurrent nonisolated func nonisolatedFunc() async {
await callee()
}
@MainActor func mainActorIsolatedFunc() async {
await callee()
}
actor SomeActor {
func someActorIsolatedFunc() async {
await callee()
}
}
nonisolated(nonsending) func callee(caller: String = #function) async {
let isolation = #isolation
print("static isolation of callee: \(isolation, default: "nonisolated") when called from: \(caller)")
}
// static isolation of callee: nonisolated when called from: nonisolatedFunc()
// static isolation of callee: Swift.MainActor when called from: mainActorIsolatedFunc()
// static isolation of callee: IsolationTests.SomeActor when called from: someActorIsolatedFunc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment