This is the imagei am new to jetpack Compose. I have opened my first jetpack compose project today. I am little confuse regarding the initial syntax. i have highlighted the curly brackets. My question is are the lambdas inside lambdas? i have went through kotlin tutorial and haven't found any such structure. A little explanation with some example will be very helpful. Thanks in advance
I am just trying to understand the mentioned code structure
Yes, these are nested lambdas. Before you get your answer you should familiar with higher-order function that is the functions which takes another function as paramenter or as return type are called higher-order functions.
A higher-order function is a function that takes functions as parameters, or returns a function.
If trailing parameter (last one) of higher-order function is itself a function then you can pass the function in the form of lambda.
For example: Here is the listener for the motion done on any view.
Now you can call the function in two ways:
By using this technique the composable functions are defined as the last parameter of
setContentis of function type that's why we use trailing-lambda using curly-brackets. As you can see in the following code ofbuttonfrommaterial-3 docs:Here you can see that the
onClickis not trailing so we can't use trailing-lambda syntax here but the trailing parameter is also of type function here we can use the syntax and you also might have noticed it:You might have noticed
@Composableannotation its telling the compiler that this is composable function and anyComposablecomponent can be called in the another function that's why thecontentparameter inButtonhas also this annotation used and hence we can call other composable within the scope of this function.