Reputation: 49
I know i can concat two lists a & b to result c like this in scala. But how can i do same thing if i do not want extra variable c and hold result in variable a.
val a = List(1,2,3)
val b = List(4,5,6)
val c = a ::: b (I do not want this extra variable c and want to hold result in val a only)
Basically appending List b to List a:
Something like [ a = a ::: b]
This is giving me error: Reassignment to val.
Upvotes: 1
Views: 402
Reputation: 4231
You can treat it as a:::b
anywhere in your code . BUT note that even if you define a new val c = a:::b
it does not allocate more memory size (as the size of a
and b
). it uses data sharing of a
and b
so it good practice to create a new val if it makes your code more readable (or makes your life easier) otherwise just use a:::b . in general it is a better practice than using a 'var'
Upvotes: 0
Reputation: 29814
A val
is final. Once a value has been assigned to it (List(1,2,3)
), a new value cannot be reassigned to it.
Use a var
Upvotes: 3