Is there a trick to using composable function refe...
# compose
m
Is there a trick to using composable function references?
Copy code
@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:
Copy code
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)?
a
known bug, afaik
agreed that both should work
👍 2
m
I didn't see it in the public issue tracker, though it's possible I'm searching for the wrong thing
a
hmm, I know I've had discussions with the team about this one but it might not be tracked. Feel free to add one and we'll dupe it if we find an existing one, or if the existing one is internal we'll dupe that against the external one
🆗 1
m
filed as https://issuetracker.google.com/issues/158892712 -- thanks for the help!
👍 1
l
missed this over the weekend. i duped the bug with the existing one. definitely want this to work but it has proven challenging. i upped the bug’s priority though as i’m not sure we were tracking this as a priority item for the alpha timeframe
m
It's not the end of the world if we're stuck with lambda expressions. IIRC, you switched to changing the code at the call site, and I can see where function references could make that interesting. Push come to shove, if you find that it's just not realistic to offer function reference support... if we could get a dedicated Lint error, or perhaps a clearer compile error, that would help.
l
yeah i think we will try and have a compiler error if we can’t provide support so we can add support eventually in a “non-breaking” way
👍 1