In Scala, List is Immutable But -
i beginner scala language. in scala, list immutable per below code:
scala> var list = list(1,2,3,4,5) // list created named ‘ list ’ list: list[int] = list(1, 2, 3, 4, 5) scala> 25 :: list // prepend cons( :: ) , here new list created. res2: list[int] = list(25, 1, 2, 3, 4, 5) scala> list // print ‘ list ’ res3: list[int] = list(1, 2, 3, 4, 5)
but,
scala> list res1: list[int] = list(1, 2, 3, 4, 5) scala> list :+= 12 // append list :+= scala> list res2: list[int] = list(1, 2, 3, 4, 5, 12)
in above example, same "list" appended. how list immutable? it's confusing me. 1 kindly explain me?
http://daily-scala.blogspot.co.uk/2010/03/implicit-operator.html
:+=
not appending, it's appending new list , reassigning variable point new list. it's equivalent list = list + 12
.
25 ++ list
making new list, not assigning anywhere.
Comments
Post a Comment