@isolated(any)

Ahh, @isolated(any). It’s an attribute of contradictions. You might see it a lot, but it’s ok to ignore it. You don’t need to use it, but I think it should be used more. It must always take an argument, but that argument cannot vary.

Confusing? Definitely. But we’ll get to it all.

nshipster.com/isolated-any/

The Swift 6.0 release introduced the @isolated(any) attribute to address the challenge of tracking function isolation in async contexts, enabling inspection of a function’s isolation (e.g., MainActor or nonisolated) via a special isolation property of type (any Actor)?.

This attribute is crucial for APIs like Task and TaskGroup, ensuring predictable scheduling, particularly for MainActor-bound tasks, by preserving execution order. While it requires await even for synchronous functions, its primary role is to provide scheduling information for API producers, not callers, who can largely ignore it.

The any argument anticipates future constraints, like specific actor types, enhancing flexibility without impacting source compatibility. Developers are encouraged to use @isolated(any) when passing isolated functions to such APIs to leverage its scheduling benefits, though it’s not essential for most use cases.


Category:

Tag:

Year: