https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Donghwan

12/15/2023, 3:31 AM
Hi. There are parts that make me wonder why it is not supported in Kotlin. I first defined the variable using the Destructuring Declaration. And if I try to destruct and substitute it again, an error will occur. Is there a way to reuse variables with destructuring ?
Copy code
var (a, b) = somePair() // OK
(a, b) = somePair() // Error !
d

Daniel Pitts

12/15/2023, 3:52 AM
There isn't a way, plus reusing variables isn't a good idea in the first place.
d

Donghwan

12/15/2023, 6:10 AM
@Daniel Pitts I wonder why it's not a good idea.
c

CLOVIS

12/15/2023, 8:26 AM
It's not a good idea because it means values change. If you take a look at the function, you cannot know in one single look what the value of
a
will be, because it could be changed anywhere in the function. I know this seems like a very small detail at first, but trust us, immutability makes everything a lot simpler. It allows writing much more complex systems while keeping individual functions simple.
plus1 3