# How can I use region-based isolation?

**URL:** <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [April 23, 2024, 5:42pm UTC](https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426 "2024-04-23T17:42:37Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Michael\_Gottesman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_gottesman/32/280_2.png) [@Michael\_Gottesman](https://forums.swift.org/u/Michael_Gottesman)\
**Post date:** [April 23, 2024, 6:32pm UTC](https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5 "2024-04-23T18:32:14Z")

</div>

> [@gwendal.roue](#):
>
> I suppose you're right, but the language does not provide such a feature.
> 
> Another point of view on the same problem is the interaction of Sendable with pre-concurrency constructs such as DispatchQueue and Thread:
> 
> ```swift
> class NonSendable { }
> 
> func run(on queue: DispatchQueue) {
> let ns = NonSendable()
> queue.async {
> // ⚠️ Capture of 'ns' with non-sendable type 'NonSendable'
> // in a `@Sendable` closure.
> _ = ns
> }
> }
> 
> func spawn() {
> let ns = NonSendable()
> Thread {
> // ⚠️ Capture of 'ns' with non-sendable type 'NonSendable'
> // in a `@Sendable` closure.
> _ = ns
> }.start()
> }
> 
> ```
> 
> I should note that a simple way to remove the warning is `nonisolated(unsafe)`:
> 
> ```swift
> class NonSendable { }
> 
> // no warning
> func run(on queue: DispatchQueue) {
> nonisolated(unsafe) let ns = NonSendable()
> queue.async {
> _ = ns
> }
> }
> 
> func spawn() {
> nonisolated(unsafe) let ns = NonSendable()
> Thread {
> _ = ns
> }.start()
> }
> 
> protocol Scheduler: Sendable {
> func schedule(_ action: @escaping @Sendable () -> Void)
> }
> 
> func run(scheduler: some Scheduler) {
> nonisolated(unsafe) let ns = NonSendable()
> scheduler.schedule {
> _ = ns
> }
> }
> 
> ```

What you have shown here is a result of us not having changed apis yet to take a transferring non-Sendable closure since transferring has not finished going through evolution. As a quick example of this in action, consider the following:

```swift
class NonSendable { }

extension Task where Failure == Never {
  // Modified Task.init() converting it to a non-Sendable transferring implementation.
  init(x: (), @_inheritActorContext @_implicitSelfCapture operation: transferring @escaping () async -> Success) {
    /* ... */
  }
}

func spawn() {
    let ns = NonSendable()
    Task.init(x: ()) {
        _ = ns
    }
}

```

---

_[View the full topic](https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426)._
