rob42
04/08/2024, 10:56 AMwhen
with sealed classes -- I'm getting an error about the when needing to be exhaustive... but it is? This only happens in my multiplatform code:
sealed class Widget {
data class Foo(val name: String) : Widget()
data class Bar(val name: String) : Widget()
fun example() {
// ERROR: 'when' expression must be exhaustive, add necessary 'else' branch
when (this) {
is Foo -> {}
is Bar -> {}
}
}
}
Any ideas?Gary Peck
04/08/2024, 11:28 AMFilip Wiesner
04/08/2024, 12:17 PMrob42
04/08/2024, 12:21 PMrob42
04/08/2024, 12:21 PMrob42
04/08/2024, 12:27 PMrob42
04/08/2024, 12:29 PMrob42
04/08/2024, 1:18 PMcommonMain/src/kotlin/
with a package com.foo.bar
line at the top
2. The module is using the android library plugin + the kotlin multiplatform plugins, and has android and jvm targets, e.g. kotlin { jvmToolchain(17); jvm(); androidTarget() }
3. The android { namespace = "com.foo.bar" }
namespace is the same as the package at the top of the source file.
Moving the source file into commonMain/src/kotlin/com/foo/bar/ causes the error to no longer appear. No idea why though.CLOVIS
04/08/2024, 1:40 PMRemi Latapy
05/17/2024, 9:16 AMwhen (val result = getProductDetailsUseCase(productId))
ok -> when (val result: LBResult<ProductDetails> = getProductDetailsUseCase(productId))
In the same project, kmp modules dont have the issue with the same sealed class used.Niall
06/29/2024, 7:55 AMpackage com.foo.bar
in folder src/commonMain/kotlin/Bar.kt
. And you have code generator like ksp which generates code into build/generated/ksp/metadata/commonMain/kotlin/com/foo/bar/Bar.kt
.
To fix this issue you have to create intermediate package folders in your you src code src/commonMain/kotlin/com/foo/bar/Bar.kt
.