Nested Property Wrappers in Swift
The Swift Evolution Proposal details how composition of property wrappers works - the
wrappedValueaccess goes two levels deep.
They work inside out, like Python decorators, but with more ceremony from the type system.
The
wrappedValueof the outer property wrapper isn’tString, it’s actuallyAppending, and therein lies the problem. If we want one level of wrapping,Appendingneeds to take aString, but for two levels of wrapping it needs to take anotherAppending.This might seem like a Catch-22, but we can actually use protocolization to solve this issue. Instead of
Appendingoperating onStrings, we can make it operate on “anything that is appendable” by using a protocol.
Previously: