Nested Property Wrappers in Swift
The Swift Evolution Proposal details how composition of property wrappers works - the
wrappedValue
access goes two levels deep.
They work inside out, like Python decorators, but with more ceremony from the type system.
The
wrappedValue
of the outer property wrapper isn’tString
, it’s actuallyAppending
, and therein lies the problem. If we want one level of wrapping,Appending
needs 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
Appending
operating onStrings
, we can make it operate on “anything that is appendable” by using a protocol.
Previously: