If I want my composable to take in a list of other...
# compose
c
If I want my composable to take in a list of other composables. For example, I want to take in a list of buttons, and depending on the state of my composable, I will show one of the buttons via
buttonList[selected]()
I thought defining an argument into my composable like this would work
buttonList: List<@Composable ColumnScope.() -> Unit>
but then sending in a
listOf(CustomButton(), CustomButton2())
doesn't actually work. Thoughts?
z
Because the value of the expression
CustomButton()
isn’t the custom button function, it is the return value of
CustomButton
. You need to either pass a function reference or a lambda.
c
@Zach Klippenstein (he/him) [MOD] thanks! If I understand correctly, I would pass in this?
Copy code
listOf(() -> CustomButton(), () -> CustomButton2())
z
That’s not valid kotlin syntax, I think those are Java lambdas? But it’s the right idea, yea
1
c
TIL that idk what lambdas are in kotlin. lol Thanks I'll search it up from here
a
Just stick some {} around the calls
c
Oh jeez. listOf({CustomButton()}, {CustomButton2()}) worked. I never really felt comfy with lambdas and functions and stuff and it bit me here. Appreciate the hand holding. Gotta re-read the Big nerd ranch kotlin book on my desk. 🙃
t
Is there any plan to support function references for composable functions in the future? Because this would be the preferred way of doing this.
a
iirc there was a kotlinc extension point that the compose plugin would need to handle function references to composable functions that isn't present today. Once that's available we'll add support, but the workaround of using a lambda is simple enough that we haven't considered it a high priority, just a nice to have for completeness