Why Can't Kotlin Infer The Type For Comparator
Asked Answered
M

1

10

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

Murmuration answered 12/9, 2018 at 9:51 Comment(3)
Interestingly it works for Collections.sort(arrayList, { x, y -> 1 })Mauricio
if sortWith would rather accept a comparator of type comparator: (T, T) -> Int it would work without specifying Comparator... 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 need Collections.sort.(this) { x, y -> comparator(x, y) } within sortedWith instead.... (I'm just thinking out loud)Wino
Very interesting read: Kotlin issue #7770 - SAM for Kotlin classes and maybe also worth a thumbs up ;-) Also: #11129 SAM conversion for kotlin function... maybe there is something coming in 1.3... didn't see it yet though...Wino
W
6

After reading the comments from the Kotlin issue 'SAM for Kotlin classes' I learned a lot regarding the SAM conversion and why typealias was introduced, but not yet why this specific behaviour wasn't solved yet... and I am not the only one as the issue and its comments show.

Summarizing, the SAM conversion was only considered for Java interfaces (compare also this comment). Jetbrains did work on (or still needs to do) a bigger refactoring and tries to solve that issue so that SAMs are also available for Kotlin functions themselves (compare also this comment). They are trying to support SAM conversion for kotlin functions in a separate issue, which could come with 1.3. As I am currently testing 1.3: I did not see anything regarding this yet. So maybe, if you like the SAM conversion as I do, you may want to upvote either SAM for Kotlin classes or SAM conversion for kotlin function or both.

By the way: a very similar example was also used by Ilya Gorbunov using arrayOf().sort.

Wino answered 12/9, 2018 at 12:21 Comment(2)
Nice find. My favourite part is "I had to create java interfaces in my kotlin project just because the lack of this feature." youtrack.jetbrains.com/issue/… So there is a workaround, at least for your own code.Mauricio
well... to be honest, I would rather want to live without workarounds ;-) I can't really understand why sortWith uses a Comparator-parameter, if we have the "nice functional types"... in this specific case I will probably rather use sort(someNumbers) { x, y -> 1 } with a Collections.sort-import... it's shorter, nicely readable and it allows me to use SAM conversion.Wino

© 2022 - 2024 — McMap. All rights reserved.