Is there a trick to using composable function references?
@Preview
@Composable
fun ListifyLambda() {
AdapterList(data = listOf("foo", "bar", "goo")) {
ListItem(it)
}
}
@Preview
@Composable
fun ListifyFnRef() {
AdapterList(data = listOf("foo", "bar", "goo"), itemCallback = ::ListItem)
}
@Composable
fun ListItem(item: String) {
Text(item)
}
Here,
ListifyLambda()
and
ListifyFnRef()
seem like they should be equivalent.
ListifyLambda()
compiles and runs without issue.
ListifyFnRef()
fails to compile, with:
Type mismatch: inferred type is KFunction1<String, Unit> but (String) -> Unit was expected
(
dev13
and AS 4.2 C1)
It doesn't seem like the Kotlin itself is flawed. Function references that match the function type should be usable, and
ListItem()
seems to match the
itemCallback
. If you try swapping compatible lambda expressions and function references outside of composables, it works as expected.
I have run into this a few times and just wrote it off as being glitchy prerelease stuff, but
https://stackoverflow.com/q/62362663/115145 spurred me to try to get to the bottom of this. Are we doing something wrong, or is this a bug/limitation of something (e.g., Kotlin compiler plugin)?