RonAvitzur
(Ron Avitzur)
1
Why is this overload an error in function scope?
func a(x: [Int]) { }
func a(x: Int...) { } // fine
func b() {
func c(x: [Int]) { }
func c(x: Int...) { } // Definition conflicts with previous value
}
4 Likes
Tino
(Tino)
2
I really, really wish the second spelling (T...) would neither be needed nor available — but this is most likely just a bug in the implementation of variadics.
It looks like the problem still exists when using simple types
(i.e. This discards any possibility that there's some weird protocol / covariance shenanigans going on)
The problem seems to be that nested functions do not support type-based overloading 
func a(x: Int) { }
func a(x: Bool) { } // Works
func a(y: Bool) { } // Works
func b() {
func c(x: Int) { }
func c(x: Bool) { } // Error: Definition conflicts with previous value
func c(y: Bool) { } // Works; Same base name, but different external parameter name compiles
}
This example and the one by @TheNamesJames work on the main branch, you can double-check with a toolchain snapshot from Swift.org - Download Swift .
Short explanation: Earlier, the compiler had a name-lookup implementation that worked at parse-time, which was responsible for handling local functions, and was buggy. Recently @Slava_Pestov did a bunch of work in removing uses of the buggy name-lookup implementation and finally deleted it. This also fixed issues related to local functions: Add test case and changelog entry for local function overloading by slavapestov · Pull Request #34246 · apple/swift · GitHub
10 Likes