Mark
02/10/2023, 3:39 AMModifier
arg after all args with no defaults. What’s the reasoning here, and am I just resigned to shifting the click handler arg to be before the modifier arg?curioustechizen
02/10/2023, 4:17 AMMark
02/10/2023, 4:19 AMbuilderAction
would be a mandatory arg but must go before Modifier
and therefore the caller cannot use a trailing lambda for the builder arg.mattinger
02/10/2023, 5:34 AM@Composable
@Preview
fun SomeComposablePreview() {
SomeComposable("foobar")
}
@Composable
fun SomeComposable(
modifier: Modifier = Modifier,
text: String,
) {
}
Mark
02/10/2023, 5:42 AMfun FooButton("foo") {
doSomethingOnClick(...)
}
So I understand the argument when the composable is accepting a composable content arg, but I’m talking about ones that don’t.mattinger
02/10/2023, 5:43 AMMark
02/10/2023, 5:43 AMmattinger
02/10/2023, 5:43 AMephemient
02/10/2023, 5:56 AMandroidx.compose.material.Button
has onClick: () -> Unit
as the first parameterMark
02/10/2023, 5:57 AMephemient
02/10/2023, 6:01 AMLayout functions SHOULD place their primary or most commonyesfunction parameter in the last parameter position to permit the use of Kotlin's trailing lambda syntax for that parameter.@Composable
androidx.compose.ui.layout.Layout
has the content: @Composable () -> Unit
in non-trailing position, but that's more consistent with the other overloads taking contents: List<@Composable () -> Unit>
parameters in the same argument position, and a MeasurePolicy
ends up in the final position instead, usually written with a SAM-converted trailing lambdaMark
02/10/2023, 6:08 AMephemient
02/10/2023, 6:10 AM@Composable
function returning Unit
is called a layout, because its only purpose to be called is for the side-effect of emitting UIMark
02/10/2023, 8:01 AM() -> Unit
as the last argument. And sure enough fits the message we get when there is an issue:
Parameters in a composable function should be ordered following this pattern: params without defaults, modifiers, params with defaults and optionally, a trailing function that might not have a default param.
Filip Wiesner
02/10/2023, 1:51 PMExcept that when people read compose code, they expect trailing lamba blocks of a composable function to be composable code. It's about conventions.That is the main argument for me. Doesn't matter if it's leaf composable or not. I expect UI elements with trailing lambda to be "composable". I don't want to check the documentation to know if it is or isn't composable and the annotation is not visible when typing parameters.
Mark
02/10/2023, 1:52 PMcompose-lints
allows for any trailing lambda, not just composable ones.Filip Wiesner
02/10/2023, 1:57 PM