# When should things be equal?

**URL:** <https://discourse.julialang.org/t/when-should-things-be-equal/59345>\
**Category:** Internals & Design\
**Created:** [April 15, 2021, 2:21pm UTC](https://discourse.julialang.org/t/when-should-things-be-equal/59345 "2021-04-15T14:21:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 15, 2021, 2:21pm UTC](https://discourse.julialang.org/t/when-should-things-be-equal/59345/1 "2021-04-15T14:21:30Z")

</div>

There is some useful (at least for me) discussion between @marius311 and I on [this issue](https://github.com/jonniedie/ComponentArrays.jl/issues/77) about when equality should hold between things.

It was a little surprising to me that in general `A == B` between two arrays always follows from `all(A .== B)` regardless of the types of `A` and `B`. Some examples of this are:

```julia
julia> [1 2 3] == [1, 2, 3]'
true

julia> [1, 2] == view([1, 2, 3], 1:2)
true

julia> using StaticArrays

julia> SVector(1, 2, 3) == [1, 2, 3]
true

```

While I can see where this would be a useful property, it makes things tricky when trying to implement equality for something like `ComponentArray`s. It seems obvious that `ComponentArray(a=1, b=2) != ComponentArray(x=1, y=2)`. However the whole point of `ComponentArray`s is that they need to work in any library that expects an `AbstractArray`. Since it seems to be part of the contract of `AbstractArray`s that `A == B` holds when the the elements of `A` and `B` are equal, I am hesitant to break from that. I just don’t have any way of guaranteeing that, say, DifferentialEquations.jl or Optim.jl isn’t using `A == B` under the hood somewhere. And since `hash(A) == hash(B)` needs to hold whenever `A == B`, we can’t just implement equality between two dissimilar `ComponentArray`s as a separate thing.

I assume equality between arrays (and things in general) has been discussed somewhere before. I would be curious to see the original rationale behind `A == B` for arrays when their elements are equal.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 15, 2021, 2:36pm UTC](https://discourse.julialang.org/t/when-should-things-be-equal/59345/2 "2021-04-15T14:36:07Z")

</div>

I only found [this comment](https://github.com/JuliaLang/julia/issues/5778#issuecomment-34908942) but probably there is a better discussion somewhere else.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 15, 2021, 2:43pm UTC](https://discourse.julialang.org/t/when-should-things-be-equal/59345/3 "2021-04-15T14:43:04Z")

</div>

> [@jonniedie](#):
>
> Since it seems to be part of the contract of `AbstractArray` s that `A == B` holds when the the elements of `A` and `B` are equal, I am hesitant to break from that.

I think the policy is that both keys and values need to be equal for two `AbstractArray`s to be equal:

```julia
julia> OffsetArray([1,2,3], 2) == [1,2,3]
false

julia> OffsetArray([1,2,3], 0) == [1,2,3]
true

```

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 15, 2021, 3:15pm UTC](https://discourse.julialang.org/t/when-should-things-be-equal/59345/4 "2021-04-15T15:15:41Z")

</div>

Ah, that makes sense. Especially since this is how `hash` on `AbstractArray`s is implemented (of course, that doesn’t work for arrays whose `keys` aren’t `LinearIndices` or `CartesianIndices` like most of the symbolic-indexed arrays like ComponentArrays, LabelledArrays, and such). So perhaps the correct way to handle this is to make them not equal. I guess that means I’ll have to trust that solvers and libraries that people are using ComponentArrays for are using `==` correctly.
