https://kotlinlang.org logo
Title
l

Loney Chou

06/24/2022, 10:45 AM
Hello, I'm having a base class like this:
abstract class Scene : JavaScene() { // JavaScene is a Java class
    private var composition: Composition? = null

    fun setContent(content: @Composable () -> Unit) {
        composition = Composition(/* ... */).apply { setContent(content) }
    }
}
and a subclass like this:
class MyScene : Scene() {
    override fun init() { // From JavaScene
        setContent { /* ... */ }
    }
}
Nothing complains during the compile time, but at the runtime when
init
gets called,
Scene.setContent
throws an error:
androidx.compose.runtime.internal.ComposableLambdaImpl cannot be cast to kotlin.jvm.functions.Function0
. How to fix that?
o

Oleksandr Balan

06/24/2022, 12:05 PM
Not sure why this happens, but did you try to not pass
content
lambda directly, but instead call it?
fun setContent(content: @Composable () -> Unit) {
    composition = Composition(/* ... */).apply { setContent { content() } }
}
l

Loney Chou

06/24/2022, 12:14 PM
Same error :(
😞 1
Honestly is this a java interop issue? I added that the
Scene
is a derived class from a java class
and that init comes from that java class
'Cause I test this with pure kotlin, nothing happens.
o

Oleksandr Balan

06/24/2022, 12:17 PM
Honestly is this a java interop issue?
Could be 🤷 Never has to work with Java & Compose 😕
l

Loney Chou

06/24/2022, 12:27 PM
Maybe derived class from java has different behavior during the compile time, and compose don't get it.
But abstract method
@Composable abstract fun content()
works properly.
🤔 1