'How can I conform a Swift actor to a protocol while preserving isolation?
Let's say we have the following goals:
- We want to use actors.
- We want to use dependency injection and resolve actors through DI.
- We want to hide our actors behind protocols so we can vary the implementation.
- We want to preserve actor isolation, otherwise there's no point in using actors.
So the specific question is: How can we conform a Swift actor to a protocol while preserving isolation?
protocol Zippity {
func foo()
}
actor Zappity: Zippity {
func foo() {} // Doesn't compile
}
I can make it compile with …
actor Zappity: Zippity {
nonisolated func foo() {}
}
… but that seems to defeat the purpose. I also tried declaring the interface method async
but that also didn't compile.
I can think of several reasonable workarounds, including composition, nonisolated async
methods that call isolated methods, etc. but wanted to see if there's something I'm missing.
Solution 1:[1]
OK, I've discovered the answer, and it's pretty straightforward:
protocol Zippity: Actor {
func foo()
}
actor Zappity: Zippity {
func foo() // This is a properly isolated method
}
It seems that declaring conformity to the Actor
protocol enables the compiler magic.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gregory Higley |