Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
actor Counter {
var value = 0
func increment() {
value += 1
}
}
let counter = Counter()
counter.increment()
print(await counter.value)It does not compile
- Expecting
1: calling an actor's method like a class method. From outside the actor every call needsawait, so the compiler refusescounter.increment(). - Expecting the call to run later and the program to print
0: Swift does not start a call to an actor withoutawait; it refuses to compile it.
- What does the program print once the call is written as
await counter.increment()? - Why can
incrementchangevaluewithoutawaitinside the actor?