# Why are there two indexOfFirst functions declared for Iterable and List types?

**URL:** <https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159>\
**Category:** Language Design\
**Created:** [June 15, 2018, 6:24am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159 "2018-06-15T06:24:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaunkawano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/shaunkawano/32/5220_2.png) [@shaunkawano](https://discuss.kotlinlang.org/u/shaunkawano)\
**Post date:** [June 15, 2018, 6:24am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/1 "2018-06-15T06:24:43Z")

</div>

Hi, it is the first time for me to ask questions in this discussion forum so if there is anything I miss please let me know so I can edit my topic.

I am curious about the fact that Kotlin API declares the same extension function `IndexOfFirst`, which has the exact same logic, for multiple types of classes: in this case, `Iterable` and `List` types.  
Below is the link for the code that I am referring to:

> <https://github.com/JetBrains/kotlin/blob/1.2.50/libraries/stdlib/common/src/generated/_Collections.kt#L272-L293>

I simply thought having one function declared into `Iterable` would be suitable enough, so I just wonder if there is specific reason why it is declared in two places.

Thanks in advance!

Shohei

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [June 15, 2018, 9:13am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/2 "2018-06-15T09:13:00Z")

</div>

Maybe it has something to do with the fact that the file is auto-generated, I don’t know 🤷‍♂️

> <https://github.com/JetBrains/kotlin/blob/1.2.50/libraries/stdlib/common/src/generated/_Collections.kt#L11-L14>

---

<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:** [June 15, 2018, 2:39pm UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/3 "2018-06-15T14:39:02Z")

</div>

Some time ago `List.indexOfFirst` was specialized to iterate list by index rather than with the `iterator()`, but then we decided it’s not entirely correct given that we don’t know whether the list supports fast indexed access, so we reverted the implementation to be the same as for `Iterable`.

---

<div class="post-metadata">

**Author:** ![shaunkawano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/shaunkawano/32/5220_2.png) [@shaunkawano](https://discuss.kotlinlang.org/u/shaunkawano)\
**Post date:** [June 18, 2018, 2:41am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/4 "2018-06-18T02:41:57Z")

</div>

Thank you!

So it means it is related to the historical implementation…

I just wonder since `List` implements `Iterable` and both classes’ implementations are the same, why not removing the implementation for `List`? (It does NOT mean I want to remove the api but I just want to know the reason why both implementation needed to exist, that’s all)

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 18, 2018, 3:25am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/5 "2018-06-18T03:25:34Z")

</div>

I’m only like 99% sure of this so if this is wrong pls correct me 😉

* * *

Let me try to explain.

```auto
interface Foo

class Bar : Foo

fun Foo.foo() ...
fun Bar.foo() ...

```

This code will be compiled to the following 2 functions in java:

```java
static void foo(Foo receiver) {}
static void foo(Bar receiver) {}

```

If you now call those functions like this

```kotlin
val b = Bar()
b.foo()
val f = b as Foo
f.foo()

```

You get the following bytecode

```auto
...
INVOKESTATIC test1/Test1Kt.foo (Ltest1/Bar;)V // this calls Bar.foo
...
INVOKESTATIC test1/Test1Kt.foo (Ltest1/Foo;)V // this calls Foo.foo

```

On the bytecode level it no longer knows that it those functions are “related”. It only knows call exactly this static function. If you now remove the version for List you can still compile, but you can no longer execute old programs without recompiling them as they still expect the function to be part of List and not part of Iterable.

---

<div class="post-metadata">

**Author:** ![j0s3f](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@j0s3f](https://discuss.kotlinlang.org/u/j0s3f)\
**Post date:** [June 19, 2018, 10:16am UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/6 "2018-06-19T10:16:37Z")

</div>

This are inline functions, so there is no call to the function in the compiled code. The code is copied there.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 19, 2018, 3:27pm UTC](https://discuss.kotlinlang.org/t/why-are-there-two-indexoffirst-functions-declared-for-iterable-and-list-types/8159/7 "2018-06-19T15:27:41Z")

</div>

Not always. The function is not marked with the internal annotation `@InlineOnly`, which means it is possible to call this function without inlining it. It could for example be called from java code which would not inline.
