Hi,
I have the following code that throws a compilation error.
Overview
- I have an
async let p1
statement - Then I have 2 statements that
await
onp1
- Removing one of those 2 awaits removes the error
Compilation error:
Immutable value 'p1' may only be initialized once
Question:
- What causes this error? (is the error a bit misleading?, or may be I don't understand it)
- How to fix this issue?
- So every time I access
p1
should I useawait p1
?
Tested on:
- Xcode Version 13.0 beta (13A5155e)
- iOS project that supports only iOS 15.0
- macOS Big Sur (11.4)
Code
func f1() {
async {
async let p1 = computePrice() //Immutable value 'p1' may only be initialized once
print(await p1)
let k = await p1
}
}
func computePrice() async -> Int {
await withCheckedContinuation { continuation in
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
continuation.resume(returning: 10)
}
}
}