-
Notifications
You must be signed in to change notification settings - Fork 49
When passing a collection to Set.contains(_:)
, forward to Set.isSuperset(of:)
#796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Set.contains(_:)
, forward to Set.isSuperset(of:)
I also want to mention that this caused a bug in our app because of a line like this: canvas.tree.elements.contains(interactionState.canvasInteractionState.selection.nodeIDs) This worked as expected for an empty selection or single selection, but evaluated to IMHO, the issue is that I’m not familiar with set theory at all, but a quick check of the Wikipedia entry for Subset shows that saying “set A contains set B” seems to be a valid statement. At least in set theory. But not in Swift. |
let s: Set = [1, 2, 3]
s.contains([1]) // true
s.contains([1] as Set) // true
s.contains([1, 2]) // false 🤨
s.contains([1, 2] as Set) // false 🤨
s.contains([1, 2, 3]) // false 🤨
s.contains([1, 2, 3] as Set) // false 🤨 This is also non-deterministic and the output may change each time the program is run, depending on the seed used for hashing. |
That’s absolutely true, it seems I forgot to point out the non-deterministic behavior explicitly. Thanks for mentioning it! |
@hamishknight I think we need to forward this to the string processing folks. |
Motivation
There is a confusing behavior of
Set.contains(_:)
when passing a collection that contains more than one element:I think this confusion is introduced by a
Collection
extension in the_StringProcessing
module:I understand that this behavior is caused by the
Set
internally storing its elements in a random order (as viewed from outside), so checking whether “[…] the collection contains the given sequence” is not really a useful thing to do on aSet
, making this method pretty much useless there.Proposed solution
To make this method useful on
Set
, it could simply forward toSet.isSuperset(of:)
, which is what I would have expected in the first place:While maybe not strictly correct in the sense of the documentation that “[…] the collection contains the given sequence” because the order of elements as passed is irrelevant, I think it is more useful because the behavior is at least deterministic.
Alternatives considered
None really. Do nothing and keep the behavior as-is, I guess.
Additional information
I posted on the Swift forum about this and received only one reply that agreed with the confusing behavior.
Here’s the thread: Passing a Collection to Set.contains(_:) leads to confusing results
The text was updated successfully, but these errors were encountered: