NdArray icon indicating copy to clipboard operation
NdArray copied to clipboard

Make index slices ExpressibleByIntegerLiteral

Open dastrobu opened this issue 3 years ago • 1 comments
trafficstars

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 avatar Feb 20 '22 13:02 dastrobu

The question will be if

let a[1] = 9

will work as expected.

dastrobu avatar Feb 21 '22 05:02 dastrobu