Why does this piece of Java code not compile in Kotlin without the explicit type parameter in Collectors.toList<String>()
? Is there a more idiomatic way to do this?
// works
List<String> folders = Files.walk(Paths.get(args[0]))
.filter(it -> it.toFile().isDirectory())
.map(it -> it.toAbsolutePath().toString())
.collect(Collectors.toList());
// does not compile - resulting type is `MutableList<in String!>..List<Any?>?` which is not compatible to `List<String>`
val folders: List<String> = Files.walk(Paths.get(args[0]))
.filter { it.toFile().isDirectory }
.map { it.toAbsolutePath().toString() }
.collect(Collectors.toList())
// compiles
val folders: List<String> = Files.walk(Paths.get(args[0]))
.filter { it.toFile().isDirectory }
.map { it.toAbsolutePath().toString() }
.collect(Collectors.toList<String>())