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
When creating a struct with some non-primitive field, e.g. of type Vector{Int64}, and overwriting the == function of the struct to just do == on their fields, the issetequal function on vectors on that struct behaves in an undesired way. As one can see in the last three lines of the code example, the issetequal is in this case not equivalent to issubset in both directions, contrary to the claim in the documentation:
Equivalent to a ⊆ b && b ⊆ a but more efficient when possible. Base.issetequal docs
julia>versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-1415:36 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU:Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
WORD_SIZE:64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
julia>struct S
x ::Vector{Int64}end
julia> Base.:(==)(s1::S, s2::S) = s1.x == s2.x
julia> a =S([])
S(Int64[])
julia> b =S([])
S(Int64[])
julia> a == b
true
julia>issetequal([a],[a])
true
julia>issetequal([a],[b])
false
julia>issubset([a],[b])
true
julia>issubset([b],[a])
true
The text was updated successfully, but these errors were encountered:
I've experienced similar results for issubset for sets. With the definitions as above
julia> a == a
true
julia>issubset([a], Set([a]))
true
julia> a == b
true
julia>issubset([a], Set([b]))
false
Due to #26198, this should stretch to issubset for vectors of length > 70 as well.
lgoettgens
changed the title
issetequal being weird with custom == on structs
issetequal and issubset being weird with custom == on structs
Jul 31, 2021
It's probably because you implemented == but not hash. To use your objects as keys in dictionaries or elements in sets, if you define == (or isequal) for them, you must also define hash.
It's mentioned in the docstring for ==:
isequal falls back to ==, so new methods of == will be used by the Dict type to compare keys. If your type will be used as a dictionary key, it should therefore also implement hash.
And in the docstring of hash:
hash(x): Compute an integer hash code such that isequal(x,y) implies hash(x)==hash(y)
Here you have:
julia>isequal(a, b)
true
julia>hash(a) ==hash(b)
false
When creating a struct with some non-primitive field, e.g. of type
Vector{Int64}
, and overwriting the==
function of the struct to just do==
on their fields, theissetequal
function on vectors on that struct behaves in an undesired way. As one can see in the last three lines of the code example, theissetequal
is in this case not equivalent toissubset
in both directions, contrary to the claim in the documentation:The text was updated successfully, but these errors were encountered: