Created
July 8, 2026 07:22
-
-
Save NikolaiRuhe/eedc554de9b1cb1861b0385b8683b3c8 to your computer and use it in GitHub Desktop.
Illustrate the fact that nonsending asynchronous functions know their isolation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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