SE-0293: Extend Property Wrappers to Function and Closure Parameters

Good question. I don't see an obvious reason not to allow it, unless the property wrapper's init(wrappedValue:) has a result builder on wrappedValue (since there can only be one result builder transformation applied to a closure).

I wonder if we should restrict the ordering of the attributes, though. Today there doesn't seem to be any such restriction for stored properties, but this bit of code is hard for me to think about:

@resultBuilder
struct Builder {
  static func buildBlock<T>(_ args: T...) -> T {
    return args.first!
  }
}

@propertyWrapper
struct Wrapper<T> {
  var wrappedValue: T
}

struct S {
  @Wrapper @Builder @Wrapper var fn: () -> Int
}

var s = S {
  1
}

Conceptually, what's happening is the result builder transformation happens on the closure first, and then the property wrapper is initialized. I wonder if it makes sense to force the programmer to write all property wrapper attributes first and the result builder attribute last, so it's more clear that the result builder is being applied to the wrapped value. It seems weird to be able to write a result builder attribute in the middle of the wrapper composition chain.

1 Like