Rooparsh
03/22/2021, 12:36 PMconst {a , …remaining} = user
…
is called spread operator in JavaScript
Where
user = {a : "12", b : 2 , c: "Some random String"}
so the value of user.a
was copied in variable a
and all the remaining keys of user object user.b
and user.c
were copied in remaining
variable.
This concept is called Destructuring
and in kotlin world, the nearest possible syntax that I know, is
val (a,b,c) = user
But, can we write this anyway better without declaring 3 variables ???
So, that my final code should be
val (a,remaining) = user
Tobias Berger
03/22/2021, 12:52 PMcomponentN()
operator functions of the object you want to destruct (component1()
for the first value, component2()
for the second and so on).
If there was a way to use something like the JS spread operator in Kotlin here, what would the type of remaining
be? The only thing I could think of is a List, probably without a useful generic type.Rooparsh
03/22/2021, 12:59 PMstephanmg
03/22/2021, 1:33 PMTobias Berger
03/22/2021, 1:36 PMstephanmg
03/22/2021, 1:38 PM