https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
f

Funyinoluwa Kashimawo

03/08/2023, 9:13 PM
I just came across array destructuring in Typescript is there something like that in kotlin?
Copy code
const numbers = [1,2,3,4,5];
const [numOne,numTwo,...remainingNumbers] = numbers;
k

Klitos Kyriacou

03/08/2023, 10:08 PM
Not only arrays, but in fact any class that defines
component1()
,
component2()
etc. can be destructured. The Kotlin equivalent to your example is:
Copy code
val numbers = arrayOf(1, 2, 3, 4, 5)
val (numOne, numTwo) = numbers
val remainingNumbers = numbers.slice(2..numbers.size)
r

rocketraman

03/08/2023, 10:15 PM
Works for
List
also
f

Funyinoluwa Kashimawo

03/09/2023, 11:48 AM
Oooh Nice, Thanks. I am aware of the Pair and Triple example but didn’t think of it as destructuring.
Thanks
5 Views