title: How to compose swift property wrappers?
body: <p>I have recently been experimenting with swift property wrappers and wondered if there was any way of composing them together in order to achieve a more modular architecture. E.g:</p>
  
  <pre><code>@WrapperOne @WrapperTwo var foo: T
  </code></pre>
  
  <p>Looking through the documentation yielded nothing. The only reference to how to do this is on 
  this GitHub page (<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md" rel="noreferrer">https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md</a>) (quotes below) which seems to say it is possible. Other articles have said that they are difficult to compose but have not explained how to go about doing this. However, I can't make heads or tails of it and would appreciate it if someone could show me some example code on how to implement this (see bottom of post).</p>
  
  <blockquote>
    <p>When multiple property wrappers are provided for a given property, the wrappers are composed together to get both effects. For example, consider the composition of <code>DelayedMutable</code> and <code>Copying</code>:</p>
  
  <pre><code>@DelayedMutable @Copying var path: UIBezierPath
  </code></pre>
    
    <p>Here, we have a property for which we can delay initialization until later. When we do set a value, it will be copied via <code>NSCopying</code>'s copy method.
    Composition is implemented by nesting later wrapper types inside earlier wrapper types, where the innermost nested type is the original property's type. For the example above, the backing storage will be of type 
    <code>DelayedMutable&lt;Copying&lt;UIBezierPath&gt;&gt;</code>
    and the synthesized getter/setter for path will look through both levels of .wrappedValue:</p>
  
  <pre><code>private var _path: DelayedMutable&lt;Copying&lt;UIBezierPath&gt;&gt; = .init()
  var path: UIBezierPath {
      get { return _path.wrappedValue.wrappedValue }
      set { _path.wrappedValue.wrappedValue = newValue }
  }
  </code></pre>
    
    <p>Note that this design means that property wrapper composition is not commutative, because the order of the attributes affects how the nesting is performed:
        @DelayedMutable @Copying var path1: UIBezierPath   // _path1 has type DelayedMutable>
        @Copying @DelayedMutable var path2: UIBezierPath   // error: _path2 has ill-formed type Copying>
    In this case, the type checker prevents the second ordering, because <code>DelayedMutable</code> does not conform to the <code>NSCopying</code> protocol. This won't always be the case: some semantically-bad compositions won't necessarily by caught by the type system. Alternatives to this approach to composition are presented in "Alternatives considered."</p>
  </blockquote>
  
  <p>Ideally, I would like to implement something like the following:</p>
  
  <pre><code>@propertyWrapper
  struct Doubled {
      var number: Int
      var wrappedValue: Int {
          get { (value * 2) }
          set { value = Int(newValue / 2) }
      }
      init(wrappedValue: Int) {
          self.number = wrappedValue
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@propertyWrapper
  struct Tripled {
      var number: Int
      var wrappedValue: Int {
          get { (value * 3) }
          set { value = Int(newValue / 3) }
      }
      init(wrappedValue: Int) {
          self.number = wrappedValue
      }
  }
  </code></pre>
  
  <p>so that this could be achieved:</p>
  
  <pre><code>@Tripled @Doubled var number = 5
  </code></pre>
  
  <p>I understand that this example is a somewhat silly reason to implement property wrapper composition but this is merely for simplicity's sake when learning a new feature.</p>
  
  <p>Any help would be greatly appreciated.</p>