https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Klitos Kyriacou

09/26/2023, 8:57 PM
Now that Kotlin 1.9.20 will work with JVM target of 21, in which
ArrayList
now has a
reversed()
method that does what Kotlin's
asReversed()
does (returns a reversed view of the original collection), will this new
reversed()
method cause any confusion with Kotlin's existing
reversed()
function which returns a new List?
e

ephemient

09/26/2023, 9:22 PM
stdlib collections extensions are supposed to have some magic so they're prioritized, this has come up before (e.g.
forEach
)
hmm well experimentally it seems to work out for a different reason
Copy code
val a: List<Any?> = ArrayList()
a.reversed() // calls Kotlin extension
(a as java.util.List<Any?>).reversed() // calls Java default method
👍 2
k

Klitos Kyriacou

09/26/2023, 9:41 PM
Similarly:
Copy code
fun foo(list: ArrayList<Int>) {
    list.reversed()  // calls Kotlin extension
    if (list is SequencedCollection<*>)
        list.reversed()  // calls Java method
}
l

Loney Chou

09/27/2023, 1:43 AM
I'd expect kotlin to have sequenced collections too. Good for unification.