https://kotlinlang.org logo
Title
e

elye

01/19/2022, 8:16 PM
Is Compose Function can’t take in Reified inline function? The below example compiles well but fail at runtime
This function has a reified type parameter and thus can only be inlined at compilation time, not called directly
@Composable
fun Launcher() {
    val context = LocalContext.current
    LazyColumn(modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally) {
        item {
            context.StartActivityButton<LaunchedEffectActivity>()
        }
    }
}

@Composable
private inline fun <reified T : Activity>Context.StartActivityButton() {
    Button(onClick = {
        startActivity(Intent(this, T::class.java))
    }) {
        Text(T::class.toString())
    }
}
More info here https://stackoverflow.com/q/70768211/3286489
m

mattinger

01/19/2022, 9:04 PM
I know that reified code has to be inlined, and that the code is essentially copied and customized (based on the parameter types) and put in the place of the original function call. (this is more or less my naive interperation of what happens). I can't imagine it being a simple process to handle compose code generation on top of the re-ification process.
🙏 1
e

elye

01/19/2022, 9:06 PM
Thanks. Perhaps it’s a limitation of Compose working with Reified then, as code can’t really be inlined for Compose code generation?
m

mattinger

01/19/2022, 9:07 PM
That's my guess. Reificiation is a somewhat complicated process is my guess. Compose does it's own code generation on top of that, so i suspect it's just too complex a thing for them to reasonably handle.
For you particular code, you might actually be able to rework it so that instead of a composable function, you generate a regular function that creates a composable
private inline fun <reified T: Activity> Context.StartActivityButton()
 : @Composable () -> Unit {
     
     return {
         Button(onClick = { 
             startActivity(Intent(this, T::class.java))
         }) {
             Text(T::class.java.toString())
         }
     }
 }
LocalContext.current.StartActivityButton<Activity>()()
it seems odd to have the double function call, but it will at least accomplish what you want. You could always do .invoke() instead of the second set of parens (they mean the same thing)
z

Zach Klippenstein (he/him) [MOD]

01/19/2022, 10:52 PM
Please file a bug. If this isn't supported, it should fail at compile time.
e

elye

01/20/2022, 3:49 AM
Thanks @Zach Klippenstein (he/him) [MOD]. I have filed a bug report at https://issuetracker.google.com/issues/215402573
🙏🏻 1