Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any difference between using "content = { }" parameter or directly "{}" in Jetpack Compose?

I was wondering if it is better to use directly "content = {}" parameter instead of "{}" despite I see more often people using "{}".

Is the code cleaner or does the code load faster by using one or the other ?
Is there any good practice ?

When using "{}"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@Composable
fun MyComposable(){
Box{}
}

When using "content = {}"

@Composable
fun MyComposable(){
Box(content = {})
}

>Solution :

The concepts used here are Kotlin trailing lambda and Kotlin named arguments.

Kotlin named arguments

So, this can be written with named arguments,

@Composable
fun MyComposable(){
    Box({})
}

like this

@Composable
fun MyComposable(){
    Box(content = {})
}

Kotlin trailing lambda

From the docs,

According to Kotlin convention, if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses

This implies,

@Composable
fun MyComposable(){
    Box({})
}

and

@Composable
fun MyComposable(){
    Box() {}
}

are same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading