I'm not sure this is really "unexpected". Swift rules for name resolution is that the "most specific" symbol gets picked. Let's examine these cases:
Test.test // works only in no arguments case
Test.test(...) // works with any number of arguments
Test().test() // zero arguments is more specific
P.test // Works for any type conforming to `P`
Test.test(...) // works with any number of arguments
Test().test() // `P` one is more specific on the number of arguments, `Test` one is more specific on the type of the caller. It seems like the language gives priority to the latter
I agree, but it does confuse me. I encountered a bug with this. At first my code only has zero arguments implemented in extension, and it works fine. Then I added a variadic one, I have never expected it will mess all my code. It is not easy to what influence it will bring. This is what I want to express here by using "unexpected".
class Test {
func test(_ values: Int...) {
print(values)
}
}
let test = Test()
test.test()
It does compile and run in Swift 5, but I'm really surprised that this is allowed in first place. Sure the array can be empty, but is variadic parameter list really allowed to be empty?
struct Something {
subscript (values: Int...) -> Void {
print(values)
}
}
let s = Something()
s[] // oh no