# Using nonisolated with wrapped properties

**URL:** <https://forums.swift.org/t/using-nonisolated-with-wrapped-properties/59622>\
**Category:** Discussion\
**Tags:** concurrency, propertywrapper, nonisolated\
**Created:** [August 12, 2022, 9:21pm UTC](https://forums.swift.org/t/using-nonisolated-with-wrapped-properties/59622 "2022-08-12T21:21:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 12, 2022, 9:21pm UTC](https://forums.swift.org/t/using-nonisolated-with-wrapped-properties/59622/1 "2022-08-12T21:21:44Z")

</div>

Consider the following code:

```swift
@propertyWrapper 
struct W {
  var wrappedValue: Int { .zero }
  var projectedValue: Int { .max }
}

@MainActor
class C {
  @W nonisolated var value // error: 'nonisolated' is not supported on properties with property wrappers

  nonisolated func test() {
    _ = value // error: main actor-isolated property 'value' can not be referenced from a non-isolated context
    _ = $value // error: main actor-isolated property '$value' can not be referenced from a non-isolated context
  }
}

```

The code currently fails to compile, because we do not support `nonisolated` on wrapped properties. This is because the accessors use the backing property (aka `_value`), which is a stored `var` so it would not be safe to mark it as `nonisolated`.

@hborla suggested to me that we could potentially allow this by making the backing property a stored `let` instead. In order to do that, we would need to provide users some way to spell it. One idea that came to my mind was letting users declared wrapped properties using a `let`, which would in turn making the backing property a `let` too.

Now, we currently don't allow placing `let` on computed properties in general, but I think perhaps we could allow it in this case, especially because the computed property transformation (and the backing property) is an implementation detail of property wrappers and when users write a wrapped property they look like a normal stored `var` rather than a computed property with explicit accessors. So letting users use a `let` in this context seems reasonable to me.

What do y'all think? I am really interested in hearing your thoughts and if you have any suggestions!

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [August 15, 2022, 9:18pm UTC](https://forums.swift.org/t/using-nonisolated-with-wrapped-properties/59622/2 "2022-08-15T21:18:46Z")

</div>

> [@suyashsrijan](#):
>
> @hborla suggested to me that we could potentially allow this by making the backing property a stored `let` instead. In order to do that, we would need to provide users some way to spell it. One idea that came to my mind was letting users declared wrapped properties using a `let`, which would in turn making the backing property a `let` too.

This seems to be consistent with wrapped function parameters, which are implicitly `let` ([SE-0053](https://github.com/apple/swift-evolution/blob/main/proposals/0053-remove-let-from-function-parameters.md)) and use a `let` constant for their backing storage ([SE-0293](https://github.com/apple/swift-evolution/blob/main/proposals/0293-extend-property-wrappers-to-function-and-closure-parameters.md#implementation-detail-property-wrappers)).

Would `nonmutating` setters on `wrappedValue` and `projectedValue` be disallowed? Does the distinction between "API-level" vs "implementation-detail" property wrappers have any relevance?

---

<div class="post-metadata">

**Author:** ![mickeyl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mickeyl/32/12258_2.png) [@mickeyl](https://forums.swift.org/u/mickeyl)\
**Post date:** [March 4, 2025, 1:30pm UTC](https://forums.swift.org/t/using-nonisolated-with-wrapped-properties/59622/3 "2025-03-04T13:30:46Z")

</div>

Just stumbled over the very same problem where I have a property wrapper that is taking care of a synchronized access as a static variable.

Has anything been done to make this easier? I don't feel like rewriting everything to use a static let Mutex.
