A range index reads a new array: values[1..3] copies the elements at 1 and
2 into a fresh array. Writing to that copy would change nothing, so Zolo
rejects a write into a slice instead of letting it vanish (it compiled and
did nothing before this diagnostic existed).
var values = [10, 20, 30, 40]
values[1..3] = [0, 0]
// ^ error[TE158]The same holds for a compound write (values[0..2] += 1), a string
(s[0..2] = "ab"), a slot of the slice (values[0..2][0] = 9) and a
destructuring slot ((values[0..2], n) = …). A field of an element written
through a slice (points[0..2][0].x = 5) is not reported: the element is
shared with the array, so that write does land.
Fix it¶
Write the elements one at a time:
var values = [10, 20, 30, 40]
for i in 1..3 {
values[i] = 0
}
print(values) // [10, 0, 0, 40]Or build the array you want and assign it to the variable itself:
var values = [10, 20, 30, 40]
values = values[..1].concat([0, 0]).concat(values[3..])