extension NonEmpty where C: MutableCollection, C.Index == Int isn't safe
ole opened this issue · comments
Your extension NonEmpty where C: MutableCollection, C.Index == Int
breaks for base collections that have integer indices that don't necessarily start at zero, such as ArraySlice
or Data
.
Example: this test crashes with "Fatal error: Index out of bounds".
func testMutableCollectionWithArraySlice() {
let numbers = Array(1...10)
var xs = NonEmpty(0, numbers[5...])
xs[1] = 43
XCTAssertEqual(43, xs[1])
}
lol you're fast, we haven't even announced this yet! 🤫
Lol, sorry. 😜 I didn't even notice this was brand new. I watched your last episode yesterday and heard you talk about non-empty collections and that inspired me to start writing this myself. When I got stuck it occurred to me I should look whether you guys had written a similar type and that's when I found this.