Swift error: ‘Sequence’ Requirements Type ‘T’ and ‘ArraysLice’ is equivalent

I am trying to update the math library to be compatible with Swift 3, but I encountered an error:

‘Sequence’ requires types’T’ and’ ArraySlice‘ is equivalent to

The Apple documentation on Sequence suggests that the makeIterator() method returns an iterator. It seems that the iterator returns an element in the grid variable, which is the variable T. I’m not so sure What am I missing here. Any comments will be helpful.

public struct Matrix 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)< br /> }
...
}

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

return AnyIterator {
if nextRowStartIndex == endInde x {
return nil
}

let currentRowStartIndex = nextRowStartIndex
nextRowStartIndex += self.columns

return self.grid[currentRowStartIndex. . }
}
}

you The code is compiled to Swift 3.1 (Xcode 8.3.3). Error

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

This happens when compiled to Swift 4 (Xcode 9, currently beta)
Sequence protocol has been defined

associatedtype Element where Self.Element == Self.Iterator.Element

This conflicts with your definition. You can choose a different
name of your type alias, or just delete it (and use T instead ):

public struct Matrix where T: FloatingPoint, T: ExpressibleByFloatLiteral {

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

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

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

extension Matrix: Sequence {
public func makeIterator() -> AnyIterator> {
let endIndex = rows * columns
var nextRowStartIndex = 0

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

let currentRowStartIndex = nextRowStartIndex
nextRowStartIndex += self.columns

return self.grid[currentRowStartIndex.. }
}
}

This compiles and runs with Swift 3 and 4.

I am trying to update the math library to be compatible with Swift 3, but I encountered an error:

'Sequence' requires type'T' and'ArraySlice< T >'Equivalent to

The Apple documentation on Sequence suggests that the makeIterator() method returns an iterator. It seems that the iterator returns an element in the grid variable, which is the variable T. I'm not quite sure I am here What is missing. Any comments will be helpful.

public struct Matrix 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> {
let endIndex = rows * columns
var nextRowStartIndex = 0

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

let currentRowStartIndex = nextRowStartIndex
nextRowStartIndex += self.columns

return self.grid[currentRowStartIndex.. }
}
}

Your code is compiled to Swift 3.1 (Xcode 8.3.3). Error

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

This happens when compiled to Swift 4 (Xcode 9, currently in beta)
Sequence protocol has been defined

associatedtype Element where Self.Element == Self.Iterator.Element

This conflicts with your definition. You can choose a different
name of your type alias, or just delete it (and use T Instead):

public struct Matrix where T: FloatingPoint, T: ExpressibleByFloatLiteral {

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

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

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

extension Matrix : Sequence {
public func makeIterator() -> AnyIterator> {
let endIndex = rows * columns
var nextRowStartIndex = 0

return AnyIterator {
if nextRo wStartIndex == endIndex {
return nil
}

let currentRowStartIndex = nextRowStartIndex
nextRowStartIndex += self.columns

return self.grid [currentRowStartIndex.. }
}
}

This compiles and runs with Swift 3 and 4.

Leave a Comment

Your email address will not be published.