MikeJfromVA
MikeJfromVA

Reputation: 513

Why do multiple funcs with same name but different external parameter names produce a compile error?

These two add methods are not overloaded (same names but different external parameter names):

func add (x:Int, y:Int) -> Int {
    return x+y
}

add(x: 1,y: 2)
add(x: 4,y: 2)

func add (_ x:Int, _ y:Int) -> Int {
    return x+y
}

add(4,5) // Delete this, and the error goes away

func add (addend1 x:Double, addend2 y:Double) -> Double {
    return x + y
}

add(addend1: 1.1, addend2: 2.2) // But the error is flagged here
add(addend1: 3.3, addend2: 4.4) // and here

The Xcode 8.2 Beta (8C30a) Playground flags the last two lines with :

Expression type 'Int' is ambiguous without more context

This should not be ambiguous (I think) because the external parameter names are all different. Weirder still, this is flagged as a Swift Compile Error, and yet they evaluate to 3.3 and 7.7 in the expression area. So it's compiling, yet... not compiling?

Is this an Xcode bug?

Upvotes: 0

Views: 75

Answers (1)

Code Different
Code Different

Reputation: 93151

It looks to me like a compiler bug. Rearranging the code fixed the problem:

func add (x:Int, y:Int) -> Int {
    return x+y
}

func add (_ x:Int, _ y:Int) -> Int {
    return x+y
}

func add (addend1 x:Double, addend2 y:Double) -> Double {
    return x + y
}

add(x: 1,y: 2)
add(x: 4,y: 2)

add(4,5)

add(addend1: 1.1, addend2: 2.2)
add(addend1: 3.3, addend2: 4.4)

I'd encourage you to file a bug report with Apple.

Upvotes: 1

Related Questions