Swift Property Wrappers
SE-0258: Property Wrappers is currently in its third review (scheduled to end yesterday, at the time of publication), and it promises to make open up functionality like
lazy
so that library authors can implement similar functionality themselves.The proposal does an excellent job outlining its design and implementation. So rather than attempt to improve on this explanation, we thought it’d be interesting to look at some new patterns that property wrappers make possible — and, in the process, get a better handle on how we might use this feature in our projects.
This is one of my favorite new Swift features, although there are some issues:
As described in the previous section, property wrappers have but two methods of recourse to deal with invalid values:
- Ignoring them (silently)
- Crashing with
fatalError()
[…]
Attempting to compose two property wrappers that act on
String
values fails, because the outermost wrapper is acting on a value of the innermost wrapper type.
Previously: