# Question about the behaviour of \`@isolated(any)\`

**URL:** <https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [July 31, 2025, 12:24am UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414 "2025-07-31T00:24:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [July 31, 2025, 12:24am UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414/1 "2025-07-31T00:24:09Z")

</div>

```swift
@globalActor
actor MyActor {
    static let shared = MyActor()
}

@MyActor
func foo(cl: @isolated(any) @Sendable () async -> Void) async {
    let actor = #isolation
    print("inside foo: \(actor)")
    print("isolation: \(cl.isolation)")
    await cl()
}

@MainActor
func f() async {
    let main = #isolation
    print("inside f: \(main)")
    await foo {
        let act = #isolation
        print("inside cl: \(act)")
    }
}

await f()

```

The code above will print:  
inside f: Optional(Swift.MainActor)  
inside foo: Optional(playground.MyActor)  
isolation: nil  
inside cl: nil

I was expecting:  
isolation: Optional(Swift.MainActor)  
inside cl: Optional(Swift.MainActor)

but the code above, I got nil.  
Am I misunderstanding the behaviour of `@isolated(any)`?

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [July 31, 2025, 1:19am UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414/2 "2025-07-31T01:19:23Z")

</div>

IIUC, the `@Sendable` annotation on the parameter effectively means the closure will be inferred to be nonisolated in this case. `@isolated(any)` doesn't affect inference rules, it just allows storing the inferred isolation within the function reference. if you want to get the 'inherit the surrounding actor context' behavior on a `@Sendable` or `sending` closure parameter, you'll need to resort to using the unformalized `@_inheritActorContext` attribute. [this thread](https://forums.swift.org/t/distinction-between-isolated-any-and-inheritactorcontext/75730/2) has some more info on the distinction between these attributes. if you add the underscored attribute, i think the isolation inference will be more like what you expect ([godbolt example here](https://swift.godbolt.org/z/zb4Ko8jd7)).

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [July 31, 2025, 10:26am UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414/3 "2025-07-31T10:26:51Z")

</div>

so, as long as I use `@Sendable` on the closure. the closure will become nonisolated?  
What about I use `@Sendable` on a function?

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [July 31, 2025, 11:13am UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414/5 "2025-07-31T11:13:29Z")

</div>

> [@carlhung](#):
>
> so, as long as I use `@Sendable` on the closure. the closure will become nonisolated?

the attribute won't necessarily cause that behavior, but in the specific given example it will have that effect. if, for example, you wanted the closure passed as the `cl` parameter to be isolated to a particular global actor, then the inclusion of `@Sendable` on the closure parameter would no longer cause the parameter to be inferred as `nonisolated`, e.g.

```swift
@MainActor
func f() async {
    let main = #isolation
    print("inside f: \(main)")
    await foo { @MainActor in // closure param in `foo()` will now be main-actor-isolated
        let act = #isolation
        print("inside cl: \(act)")
    }
}

```

> [@carlhung](#):
>
> What about I use `@Sendable` on a function?

sorry, i'm not entirely clear what you're asking about precisely. what aspects of `@Sendable` are you wondering about?

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [July 31, 2025, 12:19pm UTC](https://forums.swift.org/t/question-about-the-behaviour-of-isolated-any/81414/6 "2025-07-31T12:19:47Z")

</div>

so, If I add `@MainActor in` the closure, it will run on MainActor. Which I know it will. But without adding it, it will run on `nonisolated` by default, right?

`@Sending` on function:

```swift
@Sendable
func foo() {}

```
