Swift Error: 'Sequence' requires the types 'T' and 'ArraySlice<T>' be equivalent

Viewed 3430

I'm trying to update a math library to be compatible with Swift 3, but I'm running into an error:

'Sequence' requires the types 'T' and 'ArraySlice<T>' be equivalent

Apple's documentation on Sequence recommends that makeIterator() method returns an iterator, which it does. And it seems that the iterator is returning an element in the grid variable, which is of variable T. I'm not quite sure what I'm missing here. Any advice would be helpful.

public struct Matrix<T> where T: FloatingPoint, T: ExpressibleByFloatLiteral {
    public typealias Element = T

    let rows: Int
    let columns: Int
    var grid: [Element]

    public init(rows: Int, columns: Int, repeatedValue: Element) {
        self.rows = rows
        self.columns = columns

        self.grid = [Element](repeating: repeatedValue, count: rows * columns)
    }
... 
}

extension Matrix: Sequence { // <-- getting error here
    public func makeIterator() -> AnyIterator<ArraySlice<Element>> {
        let endIndex = rows * columns
        var nextRowStartIndex = 0

        return AnyIterator {
            if nextRowStartIndex == endIndex {
                return nil
            }

            let currentRowStartIndex = nextRowStartIndex
            nextRowStartIndex += self.columns

            return self.grid[currentRowStartIndex..<nextRowStartIndex]
        }
    }
}
1 Answers
Related