I'm writing structs to represent vectors and matrices, with some operators to work with them. But some example usages of those operators produce type errors which I can't explain.
This is a reduced excrept from my library (A was a matrix type and B a vector type, for those interested):
struct A { }
struct B { }
func *(left: A, right: A) -> A { return A() }
func *(left: A, right: B) -> B { return B() }
let a: A = A()
let b: B = (a * a) * B()
Running this in a Swift Playground produces the following compiler error:
error: Test.playground:2:20: error: 'A' is not convertible to 'B'
let b: B = (a * a) * B()
~~~~~~~~^~~~~
Inlining some of the variables, extracting a * a as a separate variable and/or removing type annotations on from the variables in some cases either resolves the error or produces a different error.
I'm not looking for a workaround, I'm trying to understand what's going on here, whether I'm doing something wrong or whether I found a bug in the compiler.
I'm running Xcode 9.2.
{ a * a }() * B()or{ $0 * $0 }(a) * B():p