Skip to content

Conversation

@ps2
Copy link

@ps2 ps2 commented Nov 20, 2018

  • PumpManagerUI vending HUDViews
  • Updates to PumpManagerStatus model
  • Multiple observers of PumpManager status updates

}
}

public class WeakObserverSet<Observer>: Sequence {
Copy link

@mpangburn mpangburn Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting approach to tackling the problem of aggregating weak references; I wasn't aware of NSHashTable.weakObjects().
However, we can implement this generically without requiring NSObject inheritance (or relying on Obj-C classes at all). Note that it's impossible to retain complete type safety when working with class-constrained protocol types as the generic parameter, as protocols don't conform to themselves*, which is the issue we're fighting here. I wrote about experiencing this problem a few months ago but didn't get much solid feedback.

That said, here's a sketch of a pure-Swift WeakSet type. It still suffers the limitation of not being able to enforce that its generic parameter is a class or class-constrained protocol at compile-time, but I think it's an improvement over the Obj-C-reliant variant.

* There are a couple exceptions here: @objc-annotated protocols self-conform, and there's an open PR in apple/swift to do the same for Error in light of recent discussion regarding the addition of Result to the stdlib. Adding a way to self-conform a protocol in Swift has come up but doesn't have concrete plans AFAIK.

@ps2 ps2 closed this Feb 26, 2019
@ps2 ps2 deleted the pumpmanager-updates branch March 5, 2019 17:46
ps2 pushed a commit that referenced this pull request Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants