[swift-users] Function overload resolution rules

Vladimir.S svabox at gmail.com
Fri Sep 30 09:40:38 CDT 2016


On 30.09.2016 15:02, Toni Suter via swift-users wrote:
> Hi,
>
> I am trying to get a better understanding of Swift's function overload
> resolution rules.
> As far as I can tell, if there are multiple candidates for a function call,
> Swift favors
> functions for which the least amount of parameters have been ignored /
> defaulted. For example:

I do believe this *should* be a bug in Swift's parser. It is not obvious 
the intention of the user, i.e. which function *user* want to call.
IMO there should be no hidden or complex rules on how Swift selects the 
candidate for function calls if parameters suits for a number of 
implementations.
IMO The only rule should be - if it is not clear from the parameter list 
which func should be called - compilation error must be thrown. This will 
be "Swifty" which is "Safe by default". Current situation is not safe by 
default.

>
> // Example 1
> func f(x: Int) {  print("f1") }
> func f(x: Int, y: Int = 0) { print("f2") }
> f(x: 0) // f1
>
> // Example 2
> func f(x: Int, y: Int = 0) {  print("f1") }
> func f(x: Int, y: Int = 0, z: Int = 0) { print("f2") }
> f(x: 0) // f1
>
> It also looks like Swift favors functions with default-value parameters
> over functions with variadic parameters:
>
> func f(x: Int = 0) {  print("f1") }
> func f(x: Int...) { print("f2") }
>
> f() // f1
> f(x: 1) // f1
> f(x: 1, 2) // f2 (makes sense because f1 would not work here)
> f(x: 1, 2, 3) // f2 (makes sense because f1 would not work here)
>
> But then I tested functions with default-value parameters and variadic
> parameters and things start to get weird.
> For example, this must be a bug, right?
>
> func f(x: Int..., y: Int = 0) { print(x, y) }
> func f(x: Int...) { print(x) }
>
> f()// []
> f(x: 1)// [1]
> f(x: 1, 2)// [1, 2] 0
> f(x: 1, 2, 3)// [1, 2, 3]
>
> I think, in this example, it should always call the second overload, because
> no parameter is ignored / defaulted. What do you think?
>
> Thanks and best regards,
> Toni
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>


More information about the swift-users mailing list