I am seeing a compiler error: Ambiguous use of init for a case that to me doesn't seem ambiguous. I have created a simplified code snippet that demonstrates the issue. Essentially, when using Generics and a pair of initializers with one and two arguments, passing two arguments gets interpreted as passing a tuple as the Generic element.
struct Point<Element> {
var x : Element! = nil
var y : Element! = nil
init(_ x : Element) {
self.x = x
}
init(_ x : Element, _ y : Element) {
self.x = x
self.y = y
}
}
let p = Point(1, 2) // this produces an Ambiguous use of init compile error
Obviously, I can resolve this by declaring the type for variable p, but the point is that the compiler actually thinks I might be passing tuple (1,2) as the argument. In fact, if I remove the second initializer, the code compiles fine and variable p is considered to have type: Point<(Int,Int> which would be equivalent if I had written instead:
struct Point<Element> {
var x : Element! = nil
var y : Element! = nil
init(_ x : Element) {
self.x = x
}
}
// the following two are being treated as equivalent
let p1 = Point(1, 2)
let p2 = Point((1,2))
To me this is a compiler bug. The compiler should treat Point(1,2) strictly as taking two arguments since I could pass Point((1,2)) if I really wanted to t pass a tuple instead.
Is this a known compiler bug, or am I thinking about this the wrong way?