Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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