Kieran Wallbanks
01/23/2025, 11:57 AMException in thread "main" java.lang.ClassCastException: class main$1 cannot be cast to class kotlin.jvm.functions.Function1 (main$1 and kotlin.jvm.functions.Function1 are in unnamed module of loader 'app')
Here are some past youtrack issues that are marked as fixed which seem fairly similar to this problem:
https://youtrack.jetbrains.com/issue/KT-67933/K2-no-conversion-between-fun-interfaces-if-target-has-suspend
https://youtrack.jetbrains.com/issue/KT-68849/K2-ClassCastException-cannot-be-cast-to-ko[…]tion2-caused-by-passing-lambda-to-SAM-constructor-results
Here's a small code example that triggers the exception:
fun interface Foo<P : Number> : suspend (P) -> Unit
class Bar<P : Number>(
foo: Foo<P>
) : Foo<P> by foo
fun <P : Number> create(foo: Foo<P>): Bar<P> = Bar(foo)
fun main() {
create<Int> {}
}
Does anyone know what could be causing this? I know that changing create to Bar(foo::invoke)
resolves the issue but this pattern is quite common in the project and the error only occurs at runtime.
The issue does not occur when the P: Number generic is removed or when the functional interface is not suspending, so it seems related to that in specific.dmitriy.novozhilov
01/23/2025, 4:05 PMKieran Wallbanks
01/23/2025, 4:20 PM