Integer generic parameters

Really exciting! On the topic of fixed-size types, rather than a runtime check, a compile-time check would be just as useful. For instance, instead of:

… perhaps there exists some variant that requires a static value, and can thus make comparisons at compile time and reject hard-coded values accordingly:

extension Vector {
    // Preferred by compiler when literals are used?
    subscript<let I: Int>(i: I) -> T where I >= 0, I < N {
        get { element(i) }
    }

    // Optional runtime fallback that may or may not be provided by the library, perhaps with some `unsafe:` marker
    subscript(i: Int) -> T {
        get {
            if i < 0 || i >= N {
                fatalError("index \(i) out of bounds [0, \(N))")
            }
            return element(i)
        }
    }
}
1 Like