Mark Murphy
06/13/2020, 8:57 PM@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)?Adam Powell
06/13/2020, 11:17 PMAdam Powell
06/13/2020, 11:18 PMMark Murphy
06/13/2020, 11:18 PMAdam Powell
06/13/2020, 11:18 PMMark Murphy
06/13/2020, 11:30 PMLeland Richardson [G]
06/15/2020, 3:57 PMMark Murphy
06/15/2020, 5:06 PMLeland Richardson [G]
06/15/2020, 5:08 PM