Created
November 8, 2020 03:45
-
-
Save godrm/826aba6ad038692df86ff4a69b576680 to your computer and use it in GitHub Desktop.
MyActor Phase#1
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
actor class MyActor { | |
let immutable: String = "42" | |
var mutableArray: [String] = [] | |
func synchronousFunction() { | |
mutableArray += ["syncFunction called"] | |
} | |
} | |
extension MyActor { | |
func asyncFunction(other: MyActor) async { | |
// allowed: an actor can access its internal state, even in an extension | |
self.mutableArray += ["asyncFunction called"] | |
// allowed: immutable memory can be accessed from outside the actor | |
print(other.immutable) | |
// error: an actor cannot access another's mutable state | |
otherActor.mutableArray += ["not allowed"] | |
// error: either reading or writing | |
print(other.mutableArray.first) | |
// allowed: async functions can call async functions on other actors | |
await other.asyncFunction(otherActor: self) | |
// error: only asynchronous functions can be called from outside the actor | |
other.synchronousFunction() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment