You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns a Boolean value that indicates whether the given element exists in the set.
Given two Sets of String (or other element), then, I would expect that s1.contains(s2) always returns false because s1 contains String elements and does not contain any Set<String> elements. But this is not the case; it actually returns true if the underlying ordering of s2 is found in the identical order in s1.
In the test case code, there are many possible permutations of output, but here are two representative lines of output:
["b", "a", "c"] contains ["b", "a"]: true
Notice in this one above, the elements of the second set ["b", "a"] appears in this exact order in the first one
["a", "c", "b"] contains ["a", "b"]: false
In this second example above, the elements of the second set do not appear in the same order in the first set (there is a "c" in between)
Reproduction
import Foundation
for_in0..<1000{lets1=Set(["a","b","c"])lets2=Set(["b","a"])print("\(s1) contains \(s2): \(s1.contains(s2))")
_ =Set<String>([]) // this seems to increase randomness of the internal ordering
}
Expected behavior
Expected: always returns false because s1 never contains the set s2 as an element
Actual: often returns true
Environment
$ swiftc -version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0
Additional information
No response
The text was updated successfully, but these errors were encountered:
northnose
changed the title
Set.contains(Set) unexpectedly sometimes returns true
Set<E>.contains(Set<E>) unexpectedly sometimes returns true
Feb 5, 2025
Description
The documentation of Set.contains(_:) states:
Given two
Set
s ofString
(or other element), then, I would expect thats1.contains(s2)
always returns false becauses1
containsString
elements and does not contain anySet<String>
elements. But this is not the case; it actually returns true if the underlying ordering ofs2
is found in the identical order ins1
.In the test case code, there are many possible permutations of output, but here are two representative lines of output:
Notice in this one above, the elements of the second set
["b", "a"]
appears in this exact order in the first oneIn this second example above, the elements of the second set do not appear in the same order in the first set (there is a "c" in between)
Reproduction
Expected behavior
Expected: always returns false because
s1
never contains the sets2
as an elementActual: often returns true
Environment
$ swiftc -version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0
Additional information
No response
The text was updated successfully, but these errors were encountered: