icanzilb
(Marin Todorov)
1
I'm trying to understand the reason for this unexpected compiler error I'm getting. Here is how to reproduce the issue:
- Make two functions with the same name - one sync and one async
- I can call each one separately.
I'm using the 'task' SwiftUI modifier which takes an async closure:
and

- But if I use any
await in the task body, calling the sync version of the function produces compiler error. The compiler complains that I need to use 'await' with async functions. For example:
(The await call doesn't need to be to the same function, also a call to Task.sleep(...) trigger the compiler error)
I don't see the reason why I can't call the sync version - is this a bug or a feature?
ksluder
(Kyle Sluder)
2
What is .task? Is this a SwiftUI view?
1 Like
icanzilb
(Marin Todorov)
3
Yes, it's the task SwiftUI modifier - it takes an async closure
ksluder
(Kyle Sluder)
4
And what is the compiler error?
1 Like
icanzilb
(Marin Todorov)
5
Need to use 'await' when calling an async function (not on the laptop rn for the exact error)
ksluder
(Kyle Sluder)
6
Ah, ok. The issue is that the async version wins out in overload resolution. The workaround is explained in SE-0296—wrap the synchronous call in a closure:
func f() async {
let f2 = {
// In a synchronous context, the non-async overload is preferred:
doSomething()
}
f2()
}
1 Like
icanzilb
(Marin Todorov)
7
Oh I see - it's covered by the last paragraph in that subsection; thanks for the link!