Tuesday, June 25, 2019

Swift Property Wrappers

Mattt Thompson:

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:

  1. Ignoring them (silently)
  2. 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.


Comments RSS · Twitter

Leave a Comment