# Can't get isequal to compare custom struct with Int

**URL:** <https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 4, 2021, 10:12am UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178 "2021-10-04T10:12:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbal/32/21934_2.png) [@pbal](https://discourse.julialang.org/u/pbal)\
**Post date:** [October 4, 2021, 10:12am UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/1 "2021-10-04T10:12:14Z")

</div>

I am having trouble getting isequal to work for a user created struct. Below is an example that shows what I’m doing. I don’t understand what I’m doing wrong.

```julia
struct MyStruct
    value::Int
end

import Base.+
(+)(x::MyStruct,y::Int) = MyStruct(x.value + y)
import Base.isless
import Base.<
isless(x::MyStruct,y::Int) = (x.value < y)::Bool
import Base.isequal
import Base.==
isequal(x::MyStruct,y::Int) = (x.value == y)::Bool
isequal(x::MyStruct,y::MyStruct) = (x.value == y.value)::Bool

using Test
ms1 = MyStruct(5)
ms2 = MyStruct(5)
@test(ms1 == ms2)
@test(ms1 + 1 == MyStruct(6))
@test(ms1 < 6)
@test_broken(ms1 == 5) # Doesn't work ???

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 4, 2021, 10:23am UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/2 "2021-10-04T10:23:55Z")

</div>

The following works for me:

```julia
struct MyStruct
    value::Int
end

import Base.+
(+)(x::MyStruct,y::Int) = MyStruct(x.value + y)
import Base.isless
import Base.<
isless(x::MyStruct,y::Int) = (x.value < y)::Bool
import Base.isequal
import Base.==
(==)(x::MyStruct,y::Int) = (x.value == y)::Bool
(==)(x::MyStruct,y::MyStruct) = (x.value == y.value)::Bool

using Test
ms1 = MyStruct(5)
ms2 = MyStruct(5)
@test(ms1 == ms2)
@test(ms1 + 1 == MyStruct(6))
@test(ms1 < 6)
@test(ms1 == 5)

```

isequal and == are different.

---

<div class="post-metadata">

**Author:** ![pbal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbal/32/21934_2.png) [@pbal](https://discourse.julialang.org/u/pbal)\
**Post date:** [October 4, 2021, 10:26am UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/3 "2021-10-04T10:26:41Z")

</div>

Thanks. (duh)

But now I’m confused, why does \< resolve to isless(), but == does not resolve to isequal?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 4, 2021, 10:35am UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/4 "2021-10-04T10:35:40Z")

</div>

I don’t know about the rationales, but the documentation states, that isequal contains special handling for floating points (NaN for example) and isless seems to be meant mainly for implementing total orders on non numeric types.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 4, 2021, 1:08pm UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/5 "2021-10-04T13:08:30Z")

</div>

Generic `==` resolves to `===`:

```julia
# core numeric operations & types
==(x, y) = x === y

```

at `share/julia/base/Base.jl`

and

```julia
isequal(x, y) = x == y

```

`<` on the other side falls back to isless:

```julia
<(x, y) = isless(x, y)

```

while there is no generic `isless`.

I don’t know if the fallback `==(x,y) = x === y` could be made type-specific to and make `==` fallback to `isequal` by default. Maybe? (that would be breaking, though). But there might be a deeper reason for that difference.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 4, 2021, 5:45pm UTC](https://discourse.julialang.org/t/cant-get-isequal-to-compare-custom-struct-with-int/69178/6 "2021-10-04T17:45:32Z")

</div>

> [@pbal](#):
>
> I am having trouble getting isequal to work for a user created struct.

See [GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash() to composite types.](https://github.com/andrewcooke/AutoHashEquals.jl) to automate these method definitions.
