There is no `lastIndex` (computed property) for `C...
# stdlib
t
There is no
lastIndex
(computed property) for
Collection
in `Collections.kt`;
lastIndex
is available for
List
. I see no reason why
Collections
cannot have
lastIndex
, when: 1.
size
is a property declared by
Collection
, so there is no issue with implementation, which is simply
size - 1
. 2.
indices
which calls
0..size - 1
under the hood is available for
Collections
, so we have no issue with "last index might be something other than size-1". Could there be something that I didn't consider?
j
What are you going to do with it? Collections are not intrinsically indexable nor can you look up elements by index.
t
but then why do we have the computed property
indices
on
Collections
?
j
It's certainly less useful than on collections which support random access, but you can still use it as a shortcut to the ordinal values which correspond to the elements in a collection. Basically as a replacement for
mapIndexed { _, index -> index }
. I have definitely used
indices
in the past for non-random access purposes, although I can't find specific examples right now.
👀 1
k
All
Iterable
types, including
Collection
, have the method
elementAt(index)
.
t
The more I think of it, the more I think that
indices
should not be declared at
Collection
s
Yeah, that does seem weird and confusing. I can't think of a use case for
indices
on an unordered collection like a set.
@jw would be very interested if you end up remembering what any of those examples were!
j
Here's some I found via
grep
. This is only applying the
map
operation, but I expect that to be the most common in the case where you want to map the ordinals to something.
Copy code
./cashapp/redwood/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt:318:                        beginControlFlow("{ %L ->", trait.parameterTypes.indices.map { CodeBlock.of("arg$it") }.joinToCode())

./aosp/androidx-main/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/PopupViewHelper.kt:134:            Layout.FLAT -> arrayOf(variants.indices.map { it + 1 }.toIntArray())

./aosp/androidx-main/sqlite/sqlite-inspection/src/androidTest/java/androidx/sqlite/inspection/test/GetSchemaTest.kt:194:                alreadyOpenDatabases.indices.map { testEnvironment.receiveEvent().databaseOpened }
./aosp/androidx-main/sqlite/sqlite-inspection/src/androidTest/java/androidx/sqlite/inspection/test/TrackDatabasesTest.kt:91:            val actual = expected.indices.map { testEnvironment.receiveEvent().databaseOpened }

./kotlin/kotlin/compiler/tests/org/jetbrains/kotlin/generators/tests/GenerateInRangeExpressionTestData.kt:80:        val valNames = expressions.indices.map { "$prefix$it" }
it's still running but you get the idea of how it can be useful to take a collection of things and map it to a list of other things based solely on the ordinals of the elements. you can achieve the very same with
(0 until size).map
, of course, but might as well use what you got
s
Or
List(size) { … }
, but I can see the appeal of using
indices
too. Thanks for sharing!