I'm not blocked on these… but I was looking for more documentation or advice to help understand these rules and conventions about when await
should be used on constructors.
@MainActor class C1 {
init() {
}
}
@MainActor func f1() async {
let _ = C1()
}
func f2() async {
let _ = await C1()
}
@MainActor class C2 {
}
@MainActor func f3() async {
let _ = C2()
}
func f4() async {
let _ = await C2()
`- warning: no 'async' operations occur within 'await' expression
}
The C1
class is MainActor
and defines an init
without async
.
The f1
function is MainActor
. I construct a C1
instance without await
. I understand this.
The f2
function is not MainActor
. I construct a C1
instance with await
. I understand this.
The C2
class is MainActor
and does not define an init
.
The f3
function is MainActor
. I construct a C2
instance without await
. I understand this.
The f4
function is not MainActor
. I construct a C2
instance with await
and see a warning. I do not understand why this warning happened.
The init
explicitly defined for C1
is not defined as async
. I still need to await
when I come from a context that might not be MainActor
: the f2
function.
The default init
implicitly defined for C2
seems to be "more sync
than sync
"? Because I can come from a context that might not be MainActor
and construct without await
: the f4
function.
There might be some clues here in [SE-0327][1]. Would the answers I was looking for be in there? I didn't find a mention in there of how those rules are meant to affect default initializers on classes.
Any more advice about that? Thanks!