Property Wrappers: Access to both enclosing 'self' and wrapper instance

I think this limitation exists in the current design of enclosing self property wrappers because there currently is no way to support mutation of an enclosing self value type through the static subscript.

Using ReferenceWritableKeyPath allows you to write a setter for the static subscript because it promises that the value being written to has reference semantics. So, the _enclosingInstance parameter doesn't need to be mutable. Since value types don't have reference semantics, you'd have to use WritableKeyPath, but in order to write to that value, you need the enclosing instance parameter to be mutable. However, inout isn't supported on subscript parameters.

1 Like

So... can we add support for inout on subscript params? If not, why? If so, when?

It depends on who this “we” is. It can be done, but it has not yet been done, because doing so takes work:

1 Like

I will also say that using inout for the enclosing instance parameter might violate the memory exclusivity issues that the static subscript was aiming to solve (discussed in the original proposal here). I think a general solution to the exclusive access issue might involve a re-think of the design of enclosing self property wrappers.

EDIT: Just to clarify, I'm not 100% sure whether or not passing inout self to the static subscript within the wrapped property setter would violate memory exclusivity.

Terms of Service

Privacy Policy

Cookie Policy