I am having some difficulty with RangeExpressions and Generic structures. I think I must be doing something wrong but for the life of me I cannot figure it out. I have simplified my code down to the following Playground which illustrates the problem.
Warning: For me this crashes XCode so please make sure you turn off autoplay before pasting this into a playground. Otherwise, you will be kinda stuck.
Anyways, here is the code in question (again, this doesn't do anything useful but it illustrates my issue):
import Foundation
struct TestStruct<T> {
var data: [T] = []
}
extension TestStruct {
subscript(n: Int) -> T {
get {
return(data[n])
}
set(newValue) {
data[n] = newValue
}
}
subscript<U: RangeExpression>(ns: U) -> TestStruct<T> where U.Bound == Int {
get {
let nRange = ns.relative(to:data)
var ret = TestStruct<T>()
for n in (nRange.lowerBound..<nRange.upperBound) {
ret.data.append(data[n])
}
return(ret)
}
}
}
// The following 4 lines work beautifully and I'm really excited about
// open ended ranges in Swift 4.
let arr = [7,6,5,4,3,2,1,0]
let x = arr[1...3]
let y = arr[..<3]
let z = arr[4...]
let a = TestStruct<Double>(data: [0.0, 1.2, 23.2, 42.0])
// This next line causes XCode to hard crash for me.
// I realize that 8 is out of bounds but I figured this would trigger
// a fatalerror when the "data[n]" is executed in the subscript. Not so ???
// I haven't changed the optimization settings from the default debug options
let b = a[0...8]
Any help with my problem or a workaround if I have discovered a bug somehow would be most appreciated. These forums are extremely helpful and I appreciate all the experts contributing.
-An excited newbie