swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.37k stars 10.34k forks source link

nonisolated property wrappers #76725

Open mattmassicotte opened 2 days ago

mattmassicotte commented 2 days ago

Motivation

This isn't an area I understand very well. But it seems like nonisolated property wrappers should be possible if the underlying wrapper type is Sendable.

@propertyWrapper
struct Wrapped<T: Sendable>: Sendable {
    var wrappedValue: T

    init(wrappedValue: T) {
        self.wrappedValue = wrappedValue
    }
}

actor MyActor {
    // if you can write this...
    nonisolated var computedValue: Int {
        get {
            0
        }
        set {
            // ... even if it tricky to implement
        }
    }

    // shouldn't you be able to write this
    // if the wrapper is Sendable?
    @Wrapped nonisolated var wrappedValue: Int = 0
}

Proposed solution

I think this code should be accepted. As is, it produces an error:

error: 'nonisolated' is not supported on properties with property wrappers

Alternatives considered

No response

Additional information

No response

jamieQ commented 1 day ago

some historical context of possible relevance:

  1. PR comment thread addressing the root problem
  2. related forums thread (also links to other related pitches/discussions)