# Overloading == with different types of operands

**URL:** <https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059>\
**Category:** Support\
**Created:** [August 6, 2017, 5:30pm UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059 "2017-08-06T17:30:17Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 17, 2018, 10:55am UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059/21 "2018-12-17T10:55:20Z")

</div>

> [@tillmannberg](#):
>
> why can we then use operators like +, -, \*, / but not ==?

Because ==/equals needs to be symmetric to work in collections. Otherwise you add `Fraction(10, 1)` to a list, and then `list.contains(10)` may or may not return true. You can’t extend the `Int` class to be equal to some random class you write.

If you want to have your own number classes, write them all yourself and give them all proper `equals` implementations, so they may be equal to themselves. For example you could make `Fraction(10,1)` to be equal to `Complex(10, 0)` and equal to `MyInt(10)`. But you would need to create that logic yourself.

The Java/Kotlin number classes have no equals cross-compatibility. There is no easy build-in way to test, if for example a double is equal to a long, because the conversion in both directions may be lossy.

---

<div class="post-metadata">

**Author:** ![tillmannberg](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@tillmannberg](https://discuss.kotlinlang.org/u/tillmannberg)\
**Post date:** [December 17, 2018, 11:25am UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059/22 "2018-12-17T11:25:57Z")

</div>

But this does not work, because I could never have `Complex(10,0) == Fraction(10, 1)`, because of the wrong compiler error. I _can_ write correct `equals` but I _cannot_ use `==`.

You should also note that `+` and `*` should also be symmetric (among other things).

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 17, 2018, 11:38am UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059/23 "2018-12-17T11:38:20Z")

</div>

> [@tillmannberg](#):
>
> I could never have `Complex(10,0) == Fraction(10, 1)` , because of the wrong compiler error.

What? Of course you can do that. The compiler error happens because you’re comparing primitive `Int`s and in that case the compiler knows that you’re doing things that are impossible to work.

---

<div class="post-metadata">

**Author:** ![tillmannberg](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@tillmannberg](https://discuss.kotlinlang.org/u/tillmannberg)\
**Post date:** [December 17, 2018, 11:58am UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059/24 "2018-12-17T11:58:08Z")

</div>

That is what one would expect, but you would get a compiler error stating: `Operator '==' cannot be applied to 'ComplexPower' and 'ActivePower'`, in my application. Using `equals` directly works.

Similarly, Kotlin tests `AssertEquals` fails but using JUnits `AssertEqual` works however, apparently because there Kotlins compiler does not do its wrong check.

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 17, 2018, 12:24pm UTC](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059/25 "2018-12-17T12:24:36Z")

</div>

This looks like a bug: [https://youtrack.jetbrains.com/issue/KT-4071](https://youtrack.jetbrains.com/issue/KT-4071)

[Previous page](https://discuss.kotlinlang.org/t/overloading-with-different-types-of-operands/4059.md?page=1)
