dastrobu / NdArray

N dimensional array package for numeric computing in swift.
https://dastrobu.github.io/NdArray/documentation/ndarray/
MIT License
24 stars 2 forks source link

Make index slices ExpressibleByIntegerLiteral #43

Closed dastrobu closed 1 year ago

dastrobu commented 2 years ago

To remove the need to convert to a slice expliclty a[Slice(1)] one can make Slice conform to ExpressibleByIntegerLiteral. This should not be done before the old index API is removed, since old slices with strides:

let a = A[0..., 2]

would be interpreted as

let a = A[0..., Slice(2)]

leading to unexpected behaviour.

/**
 Auto convert integer literals to an index slice.

       let s = a[..., 1]

 */
extension Slice: ExpressibleByIntegerLiteral {
    public typealias IntegerLiteralType = Int

    public init(integerLiteral i: Int) {
        self.init(i)
    }
}
dastrobu commented 2 years ago

The question will be if

let a[1] = 9

will work as expected.