title: Type erasure: do we risk non-reversibly losing access to kept-alive data of the instance of the erased type, when erasing the type information?
body: <p>Consider the following common simple type erasure scheme</p>
  
  <pre><code>protocol Foo {
      associatedtype Bar
      func bar() -&gt; Bar
  }
  
  struct AnyFoo&lt;Bar&gt;: Foo {
      private let _bar: () -&gt; Bar
  
      init&lt;F: Foo&gt;(_ foo: F) where F.Bar == Bar {
          _bar = foo.bar
              /* stores a reference to foo.bar,
                 so foo kept alive by ARC? */
      }
  
      func bar() -&gt; Bar {
          return _bar()
      }
  }
  </code></pre>
  
  <p>Assume the initializer argument <code>foo</code> above is (intended to be) a temporary instance of a "large" type, from which we're only interested to slice out the information blueprinted by <code>Foo</code> (i.e., the <code>bar()</code> method). </p>
  
  <pre><code>struct Huge { /* ... */ }
  
  struct Foobar: Foo {
      internal func bar() -&gt; String {
          return "foo"
      }
      let lotsOfData: Huge = Huge()
  }
  
  func getAnyFooStr() -&gt; AnyFoo&lt;String&gt; {
      let foobar = Foobar()
      return AnyFoo(foobar)
  }
  
  let anyStrFoo = getAnyFooStr()
      /* we can now access anyStrFoo.bar() (-&gt; "foo") from our
         erased type, but do lotsOfData of the underlying seemingly
         temporary Foobar() instance still "live", unreachable? */
  </code></pre>
  
  <ul>
  <li><strong>Q:</strong> Will we still keep the remaining content of <code>foo</code> alive and unreachable in memory due to the fact that closures are reference types? And if so, I assume we would never be able to reclaim access to this lost-but-alive content?</li>
  </ul>
  
  <p><em>(I tried the above with <code>Foobar</code> as a class, monitoring the (lack of a) <code>deinit</code> call, but I've confused myself so much the last hour that I need some non-self verification for this, especially for the case when <code>Foobar</code> is a value type)</em></p>
  
  <hr>
  
  <p>Xcode 8.0 / Swift 3.</p>