-1

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 "{}"

@Composable fun MyComposable(){ Box{} } 

When using "content = {}"

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@MisterPropre just something i would is if for exemple you created your own function for exemple fun MyCustomButton(text:String, onClick: ()->Unit) then i would highly suggest instead of usingkotlin trailing lamda use kotlin named argument because you may know that this lambda correspondence to onClick but another person that works in your team would not know that and they would have to look it up etc. This not a rule but a beat practice
Yes I will,. And frankly speaking, I kept that habit of always using Kotlin Named Argument. For a beginner like me, that way makes it very clear and useful. Thanks for your advice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.