I'd say (and that's purely IMHO!) we'd deprecate Data
at some point, or lower it into standard library (along with purifying the type). My Rust colleagues frequently ask me "why special Data
instead of Array<UInt8>
?" and while I have some answers to them I must admit those answers are not too convincing even for myself.
BTW, I remember we had some meta guidelines in swift like "if it wasn't in the language today would we introduce it" and perhaps "if we did it today would we do it this way" (not quite sure about the latter) - is there a list of these swift guiding principles somewhere, historic and current?
Yep, and that's the very bit that's crucial: if I attempt to, say, pass the result of array range subscript into a method that wants Array
I'd be notified right there and then about the type mismatch, at which point I'd have to make a decision of whether to convert subscript result into Array
(and pay the corresponding conversion price) or adjust the code to use Array slice (perhaps more complicated but more optimal).
Not exactly what you meant but here's a subscript wrapper that converts integer indices inside:
extension Data {
subscript(in range: Range<Int>) -> Data {
self[index(startIndex, offsetBy: range.lowerBound) ..< index(startIndex, offsetBy: range.upperBound)]
}
subscript(in range: ClosedRange<Int>) -> Data {
self[index(startIndex, offsetBy: range.lowerBound) ... index(startIndex, offsetBy: range.upperBound)]
}
}
let foo = Data([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
var bar: Data = foo[5...7]
assert(bar == Data([5, 6, 7]))
//let baz = bar[0..<2] // Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
//let baz = bar[0...1] // Fatal error: Out of bounds: index < startIndex
let baz = bar[in: 0...1] // ok
assert(baz == Data([5, 6]))
print("done")
I guess to enhance that further I can implement my own "DataSlice" and make "subscript(in:)" return that type instead of Data. Then implement relevant Data's methods/properties in DataSlice (those would route the calls to the underlying Data value, converting indices appropriately). Quite some work.
BTW, there's a minor issue in error diagnostics of "bar[0..<2]" vs "bar[0...1]" (see above). The former gives a cryptic EXC_BAD_INSTRUCTION whilst the latter a much nicer "out of bounds" crash.