Wednesday, May 13, 2020 [Tweets] [Favorites]

Nested Property Wrappers in Swift

Noah Gilmore:

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’t String, it’s actually Appending, and therein lies the problem. If we want one level of wrapping, Appending needs to take a String, but for two levels of wrapping it needs to take another Appending.

This might seem like a Catch-22, but we can actually use protocolization to solve this issue. Instead of Appending operating on Strings, we can make it operate on “anything that is appendable” by using a protocol.

Previously:

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment