Hello
I am experimenting with actors, and global actors in particular and I observe a behaviour which I do not quite understand. In the code below I have defined a simple actor and a class that belongs to a global actor. When trying to initialize them synchronously on the @MainActor, the normal actor code works with no issues but the global actor class instance produces an error
Call to global actor 'MyGlobalActor'-isolated initializer 'init(number:)' in a synchronous main actor-isolated context
Here is the code
actor Simple {
var number: Int
init(number: Int) {
self.number = number
}
}
// MyGlobalActor definition is omitted to make this example simpler
@MyGlobalActor class PartOfGlobal {
var number: Int
init(number: Int) {
self.number = number
}
}
@MainActor func initializer() {
let simple = Simple(number: 1)
let global = PartOfGlobal(number: 1) // compiler error here
}
Is this a bug or an expected behavior?
If it is not a bug, is there some way to initialize instances from a global actor without creating a Task (synchronously) ?
BR
Filip