In a kotlin <code> for Compose I saw a usage like ...
# announcements
m
In a kotlin code for Compose I saw a usage like this:
val (a, b) = fun{}
Could someone tell me please what this usage is? Is this an assignment? Why is this crazy usage even legal? Sorry I know Kotlin is great but it sometimes make me feel stupid 😞
m
it's descructuring variable declaration, you can read about it here - https://kotlinlang.org/docs/reference/multi-declarations.html
❤️ 2
d
as Milan said it is destructuring the result of
Copy code
state { DrawerState.Closed }
see https://developer.android.com/reference/kotlin/androidx/compose/package-summary#state it grabs the type of the return in the init lambda
❤️ 1
here you can see that MutableState has 2 components which end up being the destructured variables https://developer.android.com/reference/kotlin/androidx/compose/MutableState
❤️ 1
a
The function does return a variable that can be destructured, and the coder destructures it right away without extracting the returned variable into another variable. It makes the code concise, but I do agree with you, It can sometimes be hard to read.
❤️ 1
m
Does this have anything to do with inline functions? I have a hard time understanding Kotlin docs on inline functions.
d
no, the inline keyword is used to tell the kotlin compiler to embed the code of the function instead of creating an object for it
❤️ 1
so if the function is used multiple times, the code is magically copypasted by the compiler, avoiding stack allocations and everything needed to call an extra function
❤️ 1
b
Just to reiterate what Milan and David said, as long as the target implements componentN() interfaces you can destructure it.