# == for custom type and promote\_rule

**URL:** <https://discourse.julialang.org/t/for-custom-type-and-promote-rule/15806>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 2, 2018, 11:50pm UTC](https://discourse.julialang.org/t/for-custom-type-and-promote-rule/15806 "2018-10-02T23:50:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 2, 2018, 11:50pm UTC](https://discourse.julialang.org/t/for-custom-type-and-promote-rule/15806/1 "2018-10-02T23:50:06Z")

</div>

From the docs for `==`:

> New numeric types should implement this function for two arguments of the new type, and handle comparison to other types via promotion rules where possible.

So I tried this:

```julia
struct A
  x::Float64
end

struct B
  x::Int
end

Base.convert(::Type{A}, s::B) = A(float(B.x))
Base.:(==)(x::A, y::A) = x.x == y.x
Base.:(==)(x::B, y::B) = x.x == y.x
Base.promote_rule(::Type{A}, ::Type{B}) = A

```

But `A(1) == B(1)` returns false, because it is falling back to `===`.

From the documentation I was expecting that the promotion would occur here and then `A(1) == B(1)` would be true.

Did I misread the docs here? What’s the proper way to handle this? Do I have to define

```julia
Base.:(==)(x::A, y::B) = x.x == y.x
Base.:(==)(x::B, y::A) = x.x == y.x

```

as well?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 3, 2018, 1:07am UTC](https://discourse.julialang.org/t/for-custom-type-and-promote-rule/15806/2 "2018-10-03T01:07:38Z")

</div>

There is no implicit promotion for general structs, you can define e.g.

```julia
==(a::A, b::B) = ==(promote(A, B)...)
==(a::B, b::A) = A == B

```

But typically these would share some common super type (let’s say `C`) and you would define

```julia
==(a::C, b::C) = ==(promote(A, B)...)

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 3, 2018, 12:44pm UTC](https://discourse.julialang.org/t/for-custom-type-and-promote-rule/15806/3 "2018-10-03T12:44:01Z")

</div>

I see, thanks.

If there is no super type I guess I can do

```julia
==(a::Union{A,B}, b::Union{A,B}) = ==(promote(a,b)...)

```
