https://kotlinlang.org logo
#k2-early-adopters
Title
# k2-early-adopters
m

mitch

11/29/2023, 10:07 AM
I'm trying out K2 to a large JVM codebase currently.. and tests are failing at runtime due to
Exception in thread "main" java.util.NoSuchElementException: Array is empty.
This used to work in 1.9, is this a bug? or am I doing something wrong?
Copy code
tailrec suspend fun <T> doStuff(vararg items: String, fn: suspend () -> T): T = when (items.size) {
  0 -> fn()
  else -> doStuff(*items.drop(1).toTypedArray()) {
    println(items.first()) // this explodes with java.util.NoSuchElementException: Array is empty with K2
    fn()
  }
}

suspend fun main() {
  doStuff("1", "2", "3") { println("yay") } 
}
with K2
Copy code
Exception in thread "main" java.util.NoSuchElementException: Array is empty.
	at kotlin.collections.ArraysKt___ArraysKt.first(_Arrays.kt:1012)
	at MainKt$doStuff$2.invokeSuspend(Main.kt:172)
	at MainKt$doStuff$2.invoke(Main.kt)
	at MainKt$doStuff$2.invoke(Main.kt)
	at MainKt.doStuff(Main.kt:170)
	at MainKt.main(Main.kt:178)
	at MainKt$main$3.invoke(Main.kt)
	at MainKt$main$3.invoke(Main.kt)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:270)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at MainKt.main(Main.kt)
with 1.9
Copy code
3
2
1
yay
k

kirillrakhman

11/29/2023, 10:39 AM
Please report an issue.
6 Views