EDIT March 1 2016: Fair warning: this question was asked about Kotlin before 1.0.0. Things are different since Kotlin 1.0.0. See @Jayson Minard's writing below for a Kotlin 1.0.0 answer.
In Java 8 code that uses Stream, I write things like
public static void main(String... args) {
Stream<Integer> integerStream = Stream.of(1,2,3);
List<Integer> integerList = integerStream.collect(Collectors.toList());
}
But in similar code written in Kotlin, I get unexpected results.
public fun main(args: Array<String>) {
val integerStream : Stream<Int> = Stream.of(1, 2, 3)
// this is what I expect to write, like the Java 8 code, but is a compilation error:
// "required: java.util.List<in kotlin.Int> found: kotlin.MutableList<in kotlin.Int!"
// val list : java.util.List<in Int> = integerStream.collect(Collectors.toList())
// I must instead write this
val list : MutableList<in Int> = integerStream.collect(Collectors.toList())
}
Why would the return value of the Stream#collect
expression be of a different list type in the Kotlin code than in the Java code? (I'm guessing it is because of some Java Collections-specific magic in Kotlin)