Skip to content

Instantly share code, notes, and snippets.

@rnapier
Last active April 12, 2024 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnapier/cac382eb5e589e5229af4ccdc32a850e to your computer and use it in GitHub Desktop.
Save rnapier/cac382eb5e589e5229af4ccdc32a850e to your computer and use it in GitHub Desktop.
Somewhat surprising impact of non-Sendable default values
// With Strict Concurrency
class C {}
struct S {
func f() {
Task { // Surprisingly need `@MainActor in` here to make this correct
await g() // Warning: Passing argument of non-sendable type 'C' into main actor-isolated context may introduce data races
}
}
@MainActor func g(value: C = C()) {}
}
/// It is somewhat surprising to the reader that you must move the entire Task to MainActor to avoid this warning.
/// Nothing at the call site hints to why. Given the `await`, you would expect that any needed context hopping
/// would already happen. But that's because you can't see the `C()` in the call, since it's a default value.
///
/// You might imagine that IsolatedDefaultValues would resolve this, but it does not. I'm torn about whether it should.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment