[0...2]
is an array with one element which, at best, will be a Range<Int>
from 0 through 2. You can't assign that to a slice containing, say, Int
s.
x[0...2]
on the other hand is (probably) a slice, and Sliceable
only defines a get
subscript, not a setter. So even if the types were more compatible - that is, if you tried x[0...2] = 0...2
, which at least is attempting to replace a range within x
with the values of a similarly-sized collection - it still wouldn't work.
edit: as @rintaro points out, Array does support a setter subscript for ranges – so if x
were a range you could do x[0...2] = Slice(0...2)
– but it has to be a slice you assign, so I'd still go with replaceRange
.
If what you mean is you want to replace entries 0 through 2 with some values, what you want is replaceRange
, as long as your collection conforms to RangeReplaceableCollection
(which, for example, Array
does):
var x = [0,1,2,3,4,5]
var y = [200,300,400]
x.replaceRange(2..<5, with: y)
// x is now [0,1,200,300,400,5]
Note, the replaced range and y
don't have to be the same size, the collection will expand/contract as necessary.
Also, y
doesn't have to an array, it can be any kind of collection (has to be a collection though, not a sequence). So the above code could have been written as:
var x = [0,1,2,3,4,5]
var y = lazy(2...4).map { $0 * 100 }
x.replaceRange(2..<5, with: y)
edit: so, per your edit, to in-place zero out an array of any size in one go, you can do:
var x = [1.0,2.0,0.0]
// range to replace is the whole array's range,
// Repeat just generates any given value n times
x.replaceRange(indices(x), with: Repeat(count: x.count, repeatedValue: 0.0))
Adjust the range (and count of replacing entries) accordingly if you want to just zero out a subrange.
Given your example Point
class, here is how you could implement this behavior assuming it's backed by an array under the hood:
struct Point<T: FloatLiteralConvertible> {
private var _vals: [T]
init(dimensions: Int) {
_vals = Array(count: dimensions, repeatedValue: 0.0)
}
mutating func replaceRange
<C : CollectionType where C.Generator.Element == T>
(subRange: Range<Array<T>.Index>, with newElements: C) {
// just forwarding on the request - you could perhaps
// do some additional validation first to ensure dimensions
// aren't being altered...
_vals.replaceRange(subRange, with: newElements)
}
}
var x = Point<Double>(dimensions:3)
x.replaceRange(0...2, with: [1.1,2.2,3.3])