Swift Weak References and Type Erasure
In one of my side projects, two interesting Swift problems—heterogeneous arrays and weak references—collided in an interesting way.
[…]
How can we clean up the old
caller
s? We need some logic that tells us when a signal has been deallocated. But we don’t have a direct reference to any signals at all from the data model. We gave that up when we used type erasure. The only references to the signals are inside thecaller
s.That realization leads to the insight that unlocks this problem. We can capture a weak reference to the signal inside another closure that tells us whether or not a signal has been deallocated[…]