Can I deconstruct variables while assigning them? ...
# getting-started
d
Can I deconstruct variables while assigning them? Works:
var (a ,b) = somethingReturningAPair()
But what does not work:
Copy code
var a: Int
var b: Int
(a, b) = somethingReturningAPair()
Or does it only work when initalizing variables?
a
You have to initialize variables as they are not nullable
l
Yeah, kotlin supports destructuring only in declarations (including for loops and lambdas) so you can't destructure into pre-existing variables. However it is convenient to use it with
let
, so you can
Copy code
var a: Int = ... // some number
var b: Int = ... // some number

somethingReturningAPair().let { (a, b) -> 
    // a,b shadowed here 
}
v