Streams collector use in Kotlin code


#1

I am likely being very stupid in not realizing how to get round this issue. The code:

fun usingStreamSequence(l:List<Int>):List<Int> {
    return l.stream()
        .map(::f1)
        .map(::f2)
        .map(::f3)
        .collect(Collectors.toList())
  }

give the following error on the collect call:

Type inference failed. Expected type mismatch: inferred type is (MutableList<in Int!>..List<Any?>?) but List<Int> was expected

I am using IDEA 2016.02 EAP with the Kotlin 1.1.0-dev plugin but the project is a Gradle one and uses the 1.0.2 version of Kotlin. All on Debian Sid.


#2

It’s a known problem with the type inference of Stream.collect method: https://youtrack.jetbrains.com/issue/KT-11259.
Currently you have to specify type of Collectors.toList explicitly as a workaround.


#3

Ah, OK. I will do that. Thanks for letting me know quickly, that is much appreciated.