[Pitch #3] Property wrappers (formerly known as Property Delegates)

Not sure what exactly you're asking here. Property wrappers do not use protocols, there is no associatedtype involved.

You just attach @propertyWrapper to MyWrapper and make sure that wrappedValue is of type Int. If you want MyWrapper to support more than just Int you will have to expose wrapperValue's type as generic type of MyWrapper. wrappedValue is required to have the same access level as the property wrapper, MyWrapper in your case.

I assume you mean something like

protocol P {
  associatedtype Assoc
  var foo: Assoc { get }
}
struct S: P {
  @Wrapper var foo: Int = 1
}

The type of Assoc is Int here, since that's the type of the foo property.

And how can I switch which one it tracks?

You can't. For example if you attempt to force it:

struct S: P {
  typealias Assoc = Wrapper<Int>
  @Wrapper var foo: Int = 1
}

t.swift:12:8: error: type 'S' does not conform to protocol 'P'
struct S: P {
       ^
t.swift:14:16: note: candidate has non-matching type 'Int'
  @Wrapper var foo: Int = 1
               ^

The type of foo is always Int. If you want Wrapper<Int>, that's the separate implicitly generated property named _foo (in earlier versions, $foo).

2 Likes

I don't see a need for this pitch thread to stay open, especially since the third review has begun.