https://kotlinlang.org logo
#compose
Title
# compose
c

carbaj0

08/30/2020, 3:23 PM
Copy code
@Composable
private fun ZoomControls(
    zoom: Float,
    onZoomChanged: (Float) -> Unit
) {
    Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
        ZoomButton("-", onClick = { onZoomChanged(zoom * 0.8f) })
        ZoomButton("+", onClick = { onZoomChanged(zoom * 1.2f) })
    }
}

ZoomControls(zoom) {
        zoom = it.coerceIn(MinZoom, MaxZoom)
    }
This is a bit confusing since being a Composable, I expect the lambda to be a
content: @Composable () -> Unit
instead of a callback.
z

Zach Klippenstein (he/him) [MOD]

08/30/2020, 3:40 PM
Lots of composables take callback functions that aren't composables.
remember
,
Button
,
TextField
, etc. It doesn't make sense for callbacks to be composable, since they represent events, not descriptions of UI.
c

carbaj0

08/30/2020, 4:09 PM
in the case of remember it´s fine, because I don´t expect it to return a representation in the ui. when I see an Composable in Uppercase, I expect that if the last param is a trailing lambda is for taking other Composables
but it's only my interpretation jeje
z

Zach Klippenstein (he/him) [MOD]

08/30/2020, 4:14 PM
Not sure what’s confusing about this – the parameter is named following the “on*” pattern, implying that it’s a callback, and it’s not marked
@Composable
, so it’s clear it’s not a composable. If it’s seeing code using the trailing lambda syntax, like
ZoomControls(zoom) { … }
, that’s confusing, there I’d agree and would probably rather write the lambda inside the parenthesis with an explicit parameter name (I’d do this for all callbacks). It would be nice to have a lint check warning against using trailing lambda syntax for callbacks.
a

Adam Powell

08/30/2020, 4:41 PM
Fwiw you're in good company in thinking about this, we've had a decent amount of internal debate on whether the API guidelines should take a strong opinion here or not
For the same reasons
So far I'm in camp, "no restriction" but I could be swayed
For example,
onCommit
is due for a rename and a rework as it's a Unit-returning composable function where its presence or absence in the composition is significant. Same as a button or dialog. Its body lambda is not
@Composable
nor should it be. Nor would I want to add trailing fingernail clippings and avoid the trailing lambda syntax for it
Same for
launchInComposition
Consider also things like an
OnBackPressedHandler