Created
January 8, 2022 04:26
-
-
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
This file contains 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
// 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