# Why Task { } doesn't need explicit use of self?

**URL:** <https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748>\
**Category:** Using Swift\
**Tags:** concurrency, task\
**Created:** [July 7, 2022, 4:50am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748 "2022-07-07T04:50:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 7, 2022, 4:50am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/1 "2022-07-07T04:50:18Z")

</div>

Hi,

# Question

- Why doesn't unstructured task `Task { }` require explicit use of `self`?

# My understanding (could be wrong):

- Both unstructured task `Task { ... }` and detached task `Task.detached { ... }` both run asynchronously and may outlive the function from which it is called.
- Both could cause data race when used in non-isolated function

# Code

```swift
class Car {
    var price = 100
    
    func f1() {
        Task {
            price = price + 1 //Why doesn't property `price` not require explicit of use of self? wouldn't this run asynchronously?
        }

        Task.detached {
// price = price + 1 //Error: Reference to property 'price' in closure requires explicit use of 'self' to make capture semantics explicit
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![paiv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paiv/32/4041_2.png) [@paiv](https://forums.swift.org/u/paiv)\
**Post date:** [July 7, 2022, 6:55am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/2 "2022-07-07T06:55:32Z")

</div>

The closure is marked with `@_implicitSelfCapture` with the idea that the task will eventually complete, releasing captured objects. Some question that here [Explicit self not required for Task closures?](https://forums.swift.org/t/explicit-self-not-required-for-task-closures/54364/)

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 7, 2022, 7:20am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/3 "2022-07-07T07:20:33Z")

</div>

Thanks @paiv I have some doubts, please bear with me

### Doubts:

1. I couldn't find `@_implicitSelfCapture` in the documentation, should I be looking else where?

- [init(priority:operation:)](https://developer.apple.com/documentation/swift/task/init(priority:operation:)-5ltye)
- [detached(priority:operation:)](https://developer.apple.com/documentation/swift/task/detached(priority:operation:)-8a4p6)

1. Wouldn't the detached task also eventually complete?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [July 7, 2022, 7:33am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/4 "2022-07-07T07:33:44Z")

</div>

You won't find `@_implicitSelfCapture` in the documentation because it's private (hence the underscore prefix).

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 7, 2022, 8:21am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/5 "2022-07-07T08:21:26Z")

</div>

Thanks @Avi, I wish it was mentioned in the documentation that `self` is implicitly captured, because looking at `@escaping` closure it wasn't obvious.

---

<div class="post-metadata">

**Author:** ![AlexSedykh](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AlexSedykh](https://forums.swift.org/u/AlexSedykh)\
**Post date:** [July 7, 2022, 9:26am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/6 "2022-07-07T09:26:30Z")

</div>

Docs says

> The intent behind requiring `self.` when capturing `self` in an escaping closure is to warn the developer about potential reference cycles. The closure passed to `Task` is executed immediately, and the only reference to `self` is what occurs in the body. Therefore, the explicit `self.` isn't communicating useful information and should not be required.

But next here is:

> Note: The same applies to the closure passed to `Task.detached` and `TaskGroup.addTask` .

But detached Task closure is escaping  
`operation: @escaping @Sendable () async -> Success`

I think the documentation in the footnote is outdated

> <https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md#implicit-self>

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 7, 2022, 9:53am UTC](https://forums.swift.org/t/why-task-doesnt-need-explicit-use-of-self/58748/7 "2022-07-07T09:53:54Z")

</div>

Thanks @AlexSedykh for the link

I think detached task initially wasn't required to explicitly mention `self` but was later changed.

# Questions

1. Does detached task have a `self` requirement because of how it interacts with actors (it doesn't inherit the context and could run on a different executor)?
2. Is there a way to reliably detect data races? TSAN sometimes catches it and sometimes seems to miss data races. (not sure others face this issue or I am missing something)
