我想为二维泛型数组分配类型别名。(我不想创建新类型struct MatrixT<T>{}
)。所以我接下来做:
typealias MatrixT<T> = [[T]]
但是当我开始为其编写扩展时,我发现编译器实际上并不理解这MatrixT
是二维数组。它识别类型self
为[Element]
extension MatrixT {
var columnsCount: Int {
let copy = self \\compilator recognizes this as let copy: [Element] = self
let row = self[0] \\compilator recognizes this as let row: Element
return 0
}
}
但在扩展之外,Swift 编译器理解该元素MatrixT
是数组。
func testCreation() {
let matrix: MatrixT = [[0]]
let firstRow:[Int] = matrix[0] \\ correct
let columnsCount = firstRow.count
}
为什么我不能MatrixT
将扩展中的类型引用为2D数组 ( [[T]]
)?