Skip to content

Instantly share code, notes, and snippets.

@inamiy
Created January 8, 2022 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inamiy/c70841d814757b44a5f124334f0d6491 to your computer and use it in GitHub Desktop.
Save inamiy/c70841d814757b44a5f124334f0d6491 to your computer and use it in GitHub Desktop.
Abstracting Swift Actor's impl into protocol. https://twitter.com/inamiy/status/1479670050215063557
// Q. How to abstract Actor's impl into protocol?
// 1. Plain protocol + async methods
protocol MyProtocol
{
var foo: Int { get async }
func bar() async
}
// NOTE: `async` methods are always required.
actor MyActor: MyProtocol
{
var foo: Int {
get async { 1 }
}
func bar() async
{
}
}
// 2. Actor-constrained protocol
protocol MyProtocol2: Actor
{
var foo: Int { get }
func bar()
}
actor MyActor2: MyProtocol2
{
var foo: Int {
get { 1 }
}
func bar()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment