Working on a little utility to generate 65C02 ROM images, I had this thought that it would be nice to be able to express a sub-array replacement as an array assignment, something like this:
var rom = [UInt8](repeating: 0xea, count: 32768)
rom[0x6000] <= [ 0x01, 0x02, 0x03 ]
The result of the above code would be equivalent to:
It might make more sense to make this a labelled subscript, giving you something like:
rom[from: 0x6000] = [0x01, 0x02, 0x02]
You'll have to decide some sensible behaviour for the subscript getter though (rom[from: 0x6000] == ?) and decide what to do at the bounds of the array, etc.
If you really want to use the operator, it is possible
struct EditableArraySlice<T> {
var buffer: [T]
var range: Range<Int>
}
class ROM {
var buffer: [UInt8] = [UInt8](repeating: 0, count: 1024 * 1024)
subscript(index: Int) -> EditableArraySlice<UInt8> {
get {
return EditableArraySlice(buffer: buffer, range: index..<buffer.endIndex)
}
set {
precondition(newValue.buffer.count == buffer.count)
buffer = newValue.buffer
}
}
}
func <=<S: Sequence>(lhs: inout EditableArraySlice<S.Element>, rhs: S) {
var iter = rhs.makeIterator()
for index in lhs.range {
guard let next = iter.next() else { break }
lhs.buffer[index] = next
}
}
Note: The above code causes the array to be copied each time you use the operator. If you're willing to use the not-yet-stable _modify, you can remove the array copy by replacing the set with this: