0

I wonder if this is playground bug or it's supposed to work like this:

var types = ["0", "1", "2"]      // ["0","1","2"]
    types += "3"                 // ["0","1","2","3"]
    types += ["4", "5"]          // ["0","1","2","3","4","5"]
    types[3..5] = ["34"]         // ["34"]

In my opinion in last line types should contain ["0","1","2","34","5"], however playground gives different output - written on the right.

I would consider that on the right we can see only last edited stuff, but in line 2&3 we can see whole types array.

In assistant editor I get [0] "34", while it should be [3] "34" and rest of array in my opinion.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Nat
  • 12,032
  • 9
  • 56
  • 103
  • I guess it shows the result for LHS. In your last line it would be `types[3..5]` which is (after assignment) `["34"]`. It is also consistent with `[0] "34"` you get with assistant editor. – fizruk Jun 05 '14 at 11:31

2 Answers2

2

var refers to the mutable content and also you'r re assigning the values to it .

types[] - new value at index , mean it shouldn't be concatenate .

For Ex :

var types = ["0", "1", "2"]
types += "5"
types += ["4", "5"]
types[3..5] = ["34"] // Here considering the index of 3..5 (3 & 4) as one index - Assigning a single value  and replaced with the value
types

enter image description here

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
2

The reason you are seeing only ["34"] after the types[3..<5] = ["34"] line is because the assignment operator = returns the value that has been assigned.

The other lines show the whole array because the += operator returns the result of the assignment.

Cezar
  • 55,636
  • 19
  • 86
  • 87