Skip to content
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

allunique behaviour doesn't match documentation #30375

Open
karajan9 opened this issue Dec 13, 2018 · 3 comments
Open

allunique behaviour doesn't match documentation #30375

karajan9 opened this issue Dec 13, 2018 · 3 comments

Comments

@karajan9
Copy link
Contributor

Hello everyone,
allunique doesn't seem to work on my structs. The documentation says: "allunique: Return true if all values from itr are distinct when compared with isequal."

import Base: isequal

mutable struct Cart
    x::Int
    y::Int
end

isequal(c1::Cart, c2::Cart) = return (c1.x == c2.x && c1.y == c2.y)

c1 = Cart(2, 3)
c2 = Cart(2, 3)

isequal(c1, c2)  # == true
c1 ∈ [c1, c2]  # == true

allunique([c1, c2])  # == true?!

Is this expected?

julia> versioninfo()
Julia Version 1.0.2
Commit d789231e99 (2018-11-08 20:11 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Xeon(R) CPU E3-1220 V2 @ 3.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, ivybridge)
Environment:
  JULIA_NUM_THREADS = 2
@stev47
Copy link
Contributor

stev47 commented Dec 13, 2018

I agree, it's definitely misleading.
You can work around by defining your own hash function though:

Base.hash(c::Cart, h::UInt) = hash(c.y, hash(c.x, h))

The documentation of hash() says:

hash(x[, h::UInt])

Compute an integer hash code such that isequal(x,y) implies hash(x)==hash(y). The optional second argument h is a hash code
to be mixed with the result.

So if you define your own isequal() without corresponding hash() you may violate that guarantee.

@karajan9
Copy link
Contributor Author

karajan9 commented Dec 13, 2018

Ah, that's good to know, thank you!

So, which is right then, the documentation or the behavior? Or rather, which one should be right?

@NeroBlackstone
Copy link

Same problem here, documentation should improvement

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

No branches or pull requests

3 participants