Thread
#compose
    Colton Idle

    Colton Idle

    1 year ago
    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?
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    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.
    Colton Idle

    Colton Idle

    1 year ago
    @Zach Klippenstein (he/him) [MOD] thanks! If I understand correctly, I would pass in this?
    listOf(() -> CustomButton(), () -> CustomButton2())
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    That’s not valid kotlin syntax, I think those are Java lambdas? But it’s the right idea, yea
    Colton Idle

    Colton Idle

    1 year ago
    TIL that idk what lambdas are in kotlin. lol Thanks I'll search it up from here
    Adam Powell

    Adam Powell

    1 year ago
    Just stick some {} around the calls
    Colton Idle

    Colton Idle

    1 year ago
    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. 🙃
    Timo Drick

    Timo Drick

    1 year ago
    Is there any plan to support function references for composable functions in the future? Because this would be the preferred way of doing this.
    Adam Powell

    Adam Powell

    1 year ago
    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