Skip to content

Instantly share code, notes, and snippets.

@erynofwales
Created October 24, 2015 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erynofwales/61768899502b7ac83c6e to your computer and use it in GitHub Desktop.
Save erynofwales/61768899502b7ac83c6e to your computer and use it in GitHub Desktop.
Error on line 23, pointing to the size() call: Cannot convert type 'Int' to expected argument type 'Int'
protocol Matrix {
typealias ElementType
static func dimensions() -> (rows: Int, cols: Int)
static func size() -> Int
init()
subscript(row: Int, col: Int) -> ElementType { get set }
}
struct Matrix4<T: FloatingPointType> : Matrix {
static func dimensions() -> (rows: Int, cols: Int) {
return (4, 4)
}
static func size() -> Int {
let dimensions = Matrix4.dimensions()
return dimensions.rows * dimensions.cols
}
private var data: [T] = [T](count: Matrix4.size(), repeatedValue: T(0))
init() {
data = [T](count: Matrix4.size(), repeatedValue: T(0))
}
subscript(row: Int, col: Int) -> T {
get {
}
set(value) {
}
}
}
@robrix
Copy link

robrix commented Oct 24, 2015

Often, changing the definition to an equivalent one yields a different error which sheds more light on the problem (or at least narrows down which subexpression is responsible). For example, changing the definition to:

private var data: [T] = { (value: T) in
    [T](count: Matrix4.size(), repeatedValue: value)
}(T(0))

yields:

boom.swift:26:3: error: cannot invoke closure of type '(T) -> [T]' with an argument list of type '(T)'
}(T(0))
~^

With some further experimentation, it looks like Swift doesn’t like expressions involving T at this scope; by contrast, the init you wrote seems fine. Seems radar-worthy.

@lattner
Copy link

lattner commented Jan 20, 2016

This turned out to be a name lookup bug. You can work around it in Swift 2 by using "Matrix4.size()" to specifically provide the type bound, but it is now fixed for Swift 3 as of Swift.org commit b3ac017.

@lattner
Copy link

lattner commented Jan 20, 2016

That was supposed to be Matrix4 < T > .size()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment