DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
bar() // Call to method 'bar' in closure requires explicit use of 'self' to make capture semantics explicit
}
Being prompted with this error I may want to do a fix:
DispatchQueue.main.asyncAfter(deadline: .now() + 60) { [weak self] in
guard let self else {
print("won't do anything, it's too late")
return
}
self.bar()
}
whilst here it just compiles fine, so I am not alerted:
You can read the original Task proposal and review thread to see why, but the scenario you point out was considered rare and the API was designed to make the common case even lighter weight.
I don't find the proposal convincing either. E.g.:
The closure passed to Task is executed immediately
"immediately"? Probably "soon"? but definitely not "synchronously". The same could be said about a closure passed to, say, DispatchQueue.global().async closure, but that doesn't have this behaviour.
Note : The same applies to the closure passed to Task.detached and TaskGroup.addTask .
This is not what I see implemented at the moment (Task.detached / addTask closure does require explicit self capturing).
IMHO this "implicit self" exception makes the language more complicated and less consistent overall.