# Explicit self not required for Task closures?

**URL:** <https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364>\
**Category:** Using Swift\
**Tags:** closure, escapingclosure, asyncawait\
**Created:** [December 30, 2021, 8:21pm UTC](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364 "2021-12-30T20:21:33Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [February 16, 2023, 6:22pm UTC](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364/20 "2023-02-16T18:22:13Z")

</div>

This strikes me as a problem somewhat different shape than the one that the implicit `self` capture rules are meant to solve. Those diagnostics are focused on trying to prevent common causes of strong reference _cycles_, namely, an instance of a reference type which (perhaps transitively) holds on to a strong reference to a closure which captures the original instance.

But the problem where the lifetime of captured state can get extended indefinitely in a non-terminating `Task` persists quite apart from any formal reference cycles. Indeed, this issue would persist even if you never held on to the returned `Task` object at all because it's the internal runtime machinery that keeps an executing `Task` alive, not the fact of any strong reference being held onto.

As you already note, the typical `guard let self` dance used to typically satisfy the compiler's complaints about `self` captures wouldn't be a sufficient mitigation here—you'd need to make sure you are dropping your strong `self` reference through every iteration of the loop. This has been discussed before [here](https://forums.swift.org/t/caveats-of-keeping-task-instance-beyond-its-bodys-execution/62400/6) and [here](https://forums.swift.org/t/task-async-retain-cycles/49585) at the very least.

I think you're right that this pattern deserves better diagnostics but I'm not sure that simply falling back on the usual `self` capture rules are the right tool for the job.

---

_[View the full topic](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364)._
