Overloading [T] and T... in function scope

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

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 :face_with_monocle:

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 https://swift.org/download/#snapshots .

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: https://github.com/apple/swift/pull/34246

Terms of Service

Privacy Policy

Cookie Policy