[Discussion] Allowing trailing closures on property wrapper declarations

While developing a library that declares a property wrapper initializer with a closure, I noticed that using a trailing closure is not allowed by the compiler.

Searching through the forums, I found a small reference to this in the original pitch review for property wrappers:

@propertyWrapper
public struct WithCustomDerivative<T> {
	public var wrappedValue: T

	public init(
		wrappedValue: T,
		_ derivativeTransform: @escaping (T) -> T
	) {
		self.wrappedValue = wrappedValue
		// ...
	}
}

struct Foo {
	@WithCustomDerivative { $0 / 2 } // 🛑 error: Expected declaration
	var x: Float = 0
}

Using parentheses fixes the error:

struct Foo {
	@WithCustomDerivative({ $0 / 2 }) // ✅
	var x: Float = 0
}

But it doesn't seem to me that there's any ambiguity in place. Even @Douglas_Gregor concurred back in the day:

So I guess my question is... can we allow this?

6 Likes

Looks useful to me.