https://kotlinlang.org logo
#announcements
Title
# announcements
e

evanchooly

07/06/2017, 12:08 PM
you're welcome 😄
s

sridhar

07/06/2017, 12:11 PM
evanchooly: cool! what if I need those objects to be assigned in some variable. mostly like s1..s5
d

diesieben07

07/06/2017, 12:12 PM
That's the part that won't work. You cannot have variable names assigned by expression. You then need a data structure.
Such as a list or map.
e

Eugen Martynov

07/06/2017, 12:12 PM
forEach
and reflection or if they are in array then just reference by index
e

evanchooly

07/06/2017, 12:12 PM
yeah. just use the index.
👍 2
s

sridhar

07/06/2017, 12:13 PM
got it.. thanks @evanchooly @Eugen Martynov @diesieben07
r

robin

07/06/2017, 1:07 PM
You could a destructuring assignment from the list to cut down on lines some more, but you'll have to consider if that's actually more readable:
Copy code
val (s1, s2, s3, s4, s5) = listOf(1, 2, 3, 4, 5).map { Node("q$it") }
👍 2
5 Views