Returning multiple values from a when-expression

I tried to return multiple return values from a when-expression using Pair and destructuring Declarations. This is the test code:

val (a, b) = when (s) { "def" -> Pair(false, false) "abc" -> Pair(true, true) else -> Pair(false, true) }

However, this causes a compiler error:

ERROR: Rewrite at slice SCRIPT_SCOPE key: class Line_0 old value: CODE_BLOCK@800218956 new value: CODE_BLOCK@233315297
java.lang.Throwable: Rewrite at slice SCRIPT_SCOPE key: class Line_0 old value: CODE_BLOCK@800218956 new value: CODE_BLOCK@233315297
        at com.intellij.openapi.diagnostic.Logger.error(Logger.java:134)
        at org.jetbrains.kotlin.util.slicedMap.Slices.logErrorAboutRewritingNonEqualObjects(Slices.java:158)
        at org.jetbrains.kotlin.util.slicedMap.Slices.access$000(Slices.java:30)
        at org.jetbrains.kotlin.util.slicedMap.Slices$1.processRewrite(Slices.java:42)
        at org.jetbrains.kotlin.util.slicedMap.SlicedMapImpl.put(SlicedMapImpl.java:65)
        at org.jetbrains.kotlin.resolve.DelegatingBindingTrace.record(DelegatingBindingTrace.kt:95)

[...]

ERROR: Exception while analyzing expression at (1,9) in dummy.kt
org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments: Exception while analyzing expression at (1,9) in dummy.kt

This happened when trying out that test code in kotlinc-jvm via the command line. I saw this happen with Kotlin 1.7.

Any thoughts?

Seems like a bug in the compiler. And your code is just fine: Kotlin Playground: Edit, Run, Share Kotlin Code Online

1 Like

Thanks. Unfortunately I can’t reproduce this in an isolated test project that I set up in IDEA. This is weird. I’ll watch out for this, but can’t do more atm.