Skip to content

Instantly share code, notes, and snippets.

@godrm
Created November 8, 2020 03:45
Show Gist options
  • Save godrm/826aba6ad038692df86ff4a69b576680 to your computer and use it in GitHub Desktop.
Save godrm/826aba6ad038692df86ff4a69b576680 to your computer and use it in GitHub Desktop.
MyActor Phase#1
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