Sequence.flatmap changes in Kotlin 1.4

I just noticed a weird issue when trying to copy code between two projects.

In Kotlin 1.3, it was possible to call flatMap on a Sequence, i.e.

 val xx = (1 .. 10)
            .asSequence()
            .flatMap { sequenceOf(it.toFloat()) }

Was valid in 1.3, but in 1.4 produces this error:

Cannot choose among the following candidates without completing type inference:
public fun <T, R> Sequence<Int>.flatMap(transform: (Int) → Iterable<???>): Sequence<???> defined in kotlin.sequences
public fun <T, R> Sequence<Int>.flatMap(transform: (Int) → Sequence<???>): Sequence<???> defined in kotlin.sequences

It looks like the closest equivalent is now

val xx = (1 .. 10)
            .asSequence()
            .map { sequenceOf(it.toFloat()) }
            .flatten()

Sounds like a bug in the type inference, but I can’t reproduce it on 1.4.30-M1 (I have the new IR activated that may be why) nor on 1.4.10, 1.4.20 of play.kotlinlang.org.

Are you sure the code as you pasted it produce that error?

1 Like

Kotlin 1.4 introduced new overloads for flatMap in the standard library, but in order to resolve between them you need to ensure that you use Kotlin 1.4 compiler. Check the version of Kotlin compiler in your build script and the version of Kotlin plugin installed in the IDE.

2 Likes

I have the same problem, trying to move from 1.3 to 1.4 and getting the same error on flatMap.
I set in intellij: file/setting/build,execution,deployment/compiler/kotlin compiler language version to 1.4(stable)
And I still get this error.
Without a real solution we cannot move to 1.4…

@ronen.hadar
can you provide a simple reproducer, like this

https://pl.kotl.in/wvWahUEBK

The original code looks OK on Kotlin 1.4.21

Make sure as @ilya.gorbunov said that your kotlin versions for the compiler, plugin and stdlib are the right ones. Can you share your build file? Or at least a minimal non working example?

Hi,
Thank you for the fast response.
The code indeed compiles in the maven build.
However in intellij it does not.
I tried to make sure all the parameters there are correct, but something is probably missing.
Is there a way to see what are the ‘compiler, plugin and stdlib’ that a certain module compiles with ?

Thank you
Ronen

I know it looks ok, but it breaks in intellij.
The maven build works just fine however.
When I ask for declaration of the flatMap in intellij, it points me to two implementations in kotlin-sdtlib-1.4.21.jar from org.jetbrains.kotlin.

and the code it points me to are these two functions:
/**

  • Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.

Could you share a minimal project that fails?