So I was looking around seeing if kotlin had a way...
# announcements
a
So I was looking around seeing if kotlin had a way of declaring multiple variables in a single line. I saw that it's not desired for properties which I can understand with how its syntax is. But what I'm thinking about is local vars/vals and even top-level var/val declarations. Mostly local vars/vals. I also get that the syntax doesn't really allow it but I was thinking that the destructuring declaration syntax could possibly be used in some way like
var (str1, str2): String
as a way of declaring a local/top-level var/val to be of whatever type you want it to be, and only as long as you want them both to be a var or a val as I've shown (otherwise it'd be counter intuitive). Is there a hacky way of doing so, or is it just not at all desired and for what reason?
a
TL;DR No, but you can kind of do this through destructuring. First part - https://stackoverflow.com/a/40386049/3131147 Second part - https://kotlinlang.org/docs/reference/multi-declarations.html
Destructuring example looks like this:
Copy code
var (a, b, c) = arrayOf("Adam", "Alex", "Couch")
    
println(a)
println(b)
println(c)
a
I already saw those links before I came here
a
Ah okay. That's all there is to it from what I can tell.
a
Well, I was thinking of a possible implementation, but I wanted to discuss it and see how that would bode with the community of kotlin users and developers cause I often find myself running into situations where declaring multiple vars with the same type in one single go be desirable in a local setting
c
I don't think it's a huge priority because with the emphasis on immutability, most variables are declared and assigned a value on the same line, so having multiple values is not really a win
a
Yeah I guess that's true