Reading the Java interop document about SAM Conversions, I expected the Kotlin function
Collections.sortWith(comparator: kotlin.Comparator<in T> /* = java.util.Comparator<in T> */)
to be able to take a lambda function without needing to explicitly specify the parameter is a Comparator. However the following code gives type inference failed
:
val someNumbers = arrayListOf(1, 5, 2)
someNumbers.sortWith({ x, y -> 1 })
whereas:
val someNumbers = arrayListOf(1, 5, 2)
someNumbers.sortWith(Comparator { x, y -> 1 })
compiles and runs correctly
Collections.sort(arrayList, { x, y -> 1 })
– MauriciosortWith
would rather accept a comparator of typecomparator: (T, T) -> Int
it would work without specifyingComparator
... but I don't know what I should do with that information for now ;-) And that would actually not help that much, as we would then would needCollections.sort.(this) { x, y -> comparator(x, y) }
withinsortedWith
instead.... (I'm just thinking out loud) – Wino