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:
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.
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.
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)
...
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.
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.