# Equality testing

**URL:** <https://discuss.kotlinlang.org/t/equality-testing/1816>\
**Category:** Language Design\
**Created:** [June 23, 2016, 9:36am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816 "2016-06-23T09:36:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [June 23, 2016, 9:36am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/1 "2016-06-23T09:36:10Z")

</div>

It appears that:

listOf(0) == listOf(0)

evaluates true, but that:

arrayOf(0) == arrayOf(0)

evaluates false. This most definitely violates the Principle of Least Surprise. Personally I consider this an error, but thought I would open up discussion here before filing a bug report.

---

<div class="post-metadata">

**Author:** ![norswap](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/norswap/32/1737_2.png) [@norswap](https://discuss.kotlinlang.org/u/norswap)\
**Post date:** [June 23, 2016, 12:01pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/2 "2016-06-23T12:01:31Z")

</div>

AFAIK the problem is that the `equals` methods of array simply checks for reference equality, and == desugars to `equals` (with some subtlety for nulls).

Now you could special case == in case where arrays are involved

If you use the static type, which means that you will get the old, default behavior when you pass arrays as `Object`. Sure, that is much less common, but it would be a real puzzler when it happens.

Otherwise you need to perform a dynamic type check, which has overhead. That may be fine however (especially since you only have to do it with `Object`, other types can’t be arrays).

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [June 24, 2016, 8:06am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/3 "2016-06-24T08:06:49Z")

</div>

@norswap It is indeed the value equality vs identity equality thing. The problem here is this unexposed inconsistency between List and Array, both are classes and yet because of how they are implemented == behaves so very differently. Similar things with the spread operator. The problem is less the operator semantics, than the inconsistency at the programmer level.

---

<div class="post-metadata">

**Author:** ![norswap](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/norswap/32/1737_2.png) [@norswap](https://discuss.kotlinlang.org/u/norswap)\
**Post date:** [June 24, 2016, 11:38am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/4 "2016-06-24T11:38:32Z")

</div>

I’m not sure it’s correct to say that array are classes. They just share the syntax in Kotlin. I’m not sure this was a judicious decision, seeing how array are different beasts.

The Array/List inconsistency is something inherited from the JVM, it would take a lot of beating around the bush to even approximate consistency between Array and List (another one: array don’t have an immutable view).

I think that the key principle here is compatibility with Java and that, as much as possible, things should behave as in Java. Equality behaving differently in Java and Kotlin code would be a departure from that principle in my opinion.

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [June 24, 2016, 11:53am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/5 "2016-06-24T11:53:21Z")

</div>

I agree this is a difficult problem with no easy answer now. I suspect it may be too late to fix now; measures could be taken to make the distinction clearer.

---

<div class="post-metadata">

**Author:** ![voddan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/voddan/32/1673_2.png) [@voddan](https://discuss.kotlinlang.org/u/voddan)\
**Post date:** [July 1, 2016, 7:25am UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/6 "2016-07-01T07:25:13Z")

</div>

Afaik the JB position on that is not to use arrays when possible. But I also would like a data structure with fixed size but mutable elements, e.i with `set` but without `add`

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [July 1, 2016, 5:09pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/7 "2016-07-01T17:09:49Z")

</div>

I may be missing something, but you can only use the spread operator with Array, not with List, this means using Array in some places of this code so it is better to use Array everywhere, except that you then have to worry about equals and hash.

---

<div class="post-metadata">

**Author:** ![voddan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/voddan/32/1673_2.png) [@voddan](https://discuss.kotlinlang.org/u/voddan)\
**Post date:** [July 1, 2016, 5:34pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/8 "2016-07-01T17:34:51Z")

</div>

That’s the only thing you can do with arrays that you can’t with lists. Many handy stdlib functions are not implemented for arrays because that would take too many code since all specialised array functions are auto-generated not to use generics and avoid boxing

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [July 2, 2016, 1:11pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/9 "2016-07-02T13:11:45Z")

</div>

It would be interesting to benchmark Array vs List performance to see if once can simply forget Array (even though this means losing spread operator, which is a fantastic tool).

---

<div class="post-metadata">

**Author:** ![voddan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/voddan/32/1673_2.png) [@voddan](https://discuss.kotlinlang.org/u/voddan)\
**Post date:** [July 2, 2016, 1:26pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/10 "2016-07-02T13:26:24Z")

</div>

What’s up with the spread operator? `List.toTypedArray` allows to convert with a simple copying. How often do you use `vararg` and the spread operator anyway?

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [July 2, 2016, 2:19pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/11 "2016-07-02T14:19:56Z")

</div>

Coming from a Groovy background, I use the spread operator a lot manipulating lists. I guess I could flatMap everything but that seems excessive.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [July 3, 2016, 7:13pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/12 "2016-07-03T19:13:42Z")

</div>

I hope this helps you.

> <https://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster/16565376#16565376>

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [July 4, 2016, 2:04pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/13 "2016-07-04T14:04:48Z")

</div>

By the way spread operator for collections is being considered in [https://youtrack.jetbrains.com/issue/KT-12663](https://youtrack.jetbrains.com/issue/KT-12663).

---

<div class="post-metadata">

**Author:** ![Russel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/russel/32/1442_2.png) [@Russel](https://discuss.kotlinlang.org/u/Russel)\
**Post date:** [July 4, 2016, 2:32pm UTC](https://discuss.kotlinlang.org/t/equality-testing/1816/14 "2016-07-04T14:32:27Z")

</div>

Thanks for letting us know. I have voted for this, it makes so much sense for at least lists as well as arrays.
