In the follow code:
import rx.lang.kotlin.toObservable
fun main(vararg args: String) {
val csv = listOf(
arrayOf("key", "value1"),
arrayOf("", "value2")
).toObservable()
csv
.reduce(listOf<Array<String>>()) { list, row ->
println("row ${listOf(*row)}")
if (row[0].length == 0) {
// java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
val key = list.last().get(0)
println("key $key")
row[0] = key
}
list.plus(row) // required cast: as List<Array<String>>
}.forEach { println(it) }
}
compiler force me to cast “list.plus(row) as List<Array>” (inferred as List!)!) ), if I fix this error then run the application produces a ClassCastException.
How can I fix the code?
Thanks,
Vasco
PS: build.gradle
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName='TestKt'
buildscript {
ext.kotlin_version = '1.0.2-1'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'io.reactivex:rxkotlin:0.60.0'
}
repositories {
mavenCentral()
}