Skip to content

Get rid of special-casing of ranges in == and isequal() for AbstractArrays #16364

@nalimilan

Description

@nalimilan

The AbstractArray default == operator special-cases ranges:

function (==)(A::AbstractArray, B::AbstractArray)
    if size(A) != size(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

isequal is defined in the same way.

This is obviously not great. A new array type is considered as equal to a standard Array if their elements are all equal, but it is considered as different from a range containing the same elements. This inconsistency is visible even inside Base:

julia> sparse([1,2]) == [1,2]
true

julia> [1,2] == 1:2
false

So it's not clear whether two AbstractArrays need to be of the same type or not to be ==. I can see two consistent solutions:

  1. remove the special-case for ranges (i.e. return true when elements are equal)
  2. change if isa(A,Range) != isa(B,Range) to typeof(A) !== typeof(B)

The second choice would clearly be much more disruptive than the first, and it would make == almost useless for arrays.

Metadata

Metadata

Assignees

Labels

breakingThis change will break code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions