Equality of two `AbstractArray`s

Hi,

Quick question: for x= [1, 2, 3], why is that

julia> x == 1:3
false

but

julia> x == collect(1:3)
true

?

This is on

Julia Version 0.5.1-pre+33
Commit 20d401c (2017-01-29 21:00 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin15.6.0)
  CPU: Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  BLAS: libgfortblas
  LAPACK: liblapack
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)

Doing @edit x == 1:3 results in this call in abstractarray.jl

function (==)(A::AbstractArray, B::AbstractArray)
    if indices(A) != indices(B)
        return false
    end
    if isa(A,Range) != isa(B,Range)
        return false
    end
    for (a, b) in zip(A, B)
        if !(a == b)
            return false
        end
    end
    return true
end

Now we have that

indices(x) == indices(1:3)

but

isa(x,Range) != isa(1:3,Range)``` 

returns `true` and so 

```x == 1:3```

has to be false.

Why has this decision been made?

Thanks!

Davide
1 Like

This is something we’d like to change. See: https://github.com/JuliaLang/julia/issues/16364

TL/DR: Equality and hashing must correspond with each other. For ranges, there’s a O(1) shortcut for hashing: just hash the start, step and length together. But this makes that hash different from the hash of the equivalent Array… and as such, they must compare as different.

1 Like

and as such, they must compare as different.

Isn’t there a special isequal operation for this?

Yes, but == and isequal agree except for floating-point numbers.