Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin overload resolution ambiguity in the standard library

Tags:

kotlin

In Kotlin 0.12.1230 it appears that deprecated APIs are blocking the use of their replacements.

For example, the compiler complains about the following snippet because it "cannot choose among... candidates without completing type inference"

val seq = sequenceOf("1")
val first = seq.firstOrNull()

The candidates are Sequence<T>.firstOrNull and Stream<T>.firstOrNull both of which have identical signatures and Sequence<T> extends Stream<T>. Furthermore, Stream<T> is deprecated in favor of Sequence<T>.

Attempting to resolve the type inference ambiguity, like you see below, results in the compiler complaining about "overload resolution ambiguity".

val seq = sequenceOf("1")
val first = seq.firstOrNull<String?>()

Is there any way to resolve the ambiguity while we wait for deprecated APIs to disappear entirely?

It seems that casting to the least specific type, in this case the deprecated type Stream<T>, accomplishes it, but now my code explicitly depends on a deprecated type when I have no desire to do so:

val seq = sequenceOf("1")
val first = (seq as Stream<String>).firstOrNull()

Hopefully there is a better way?

like image 509
Jonathan Schneider Avatar asked Sep 05 '25 03:09

Jonathan Schneider


1 Answers

This seems to be caused by multiple conflicting versions of the Kotlin stdlib on my classpath (caused by a long standing defect in Gradle IntelliJ integration). Once they were version conflict resolved, the compiler no longer complains.

like image 140
Jonathan Schneider Avatar answered Sep 07 '25 17:09

Jonathan Schneider