2

Unable to overload function in viewDidLoad() in swift. It gives error definition conflict with previous value" at "func joinString(#strings: String...) -> String {

override func viewDidLoad() {
    super.viewDidLoad()
    func joinString(#strings: String[]) -> String {
            var str = "" 
            for s in strings {
                str += s
            }
            return str
        }
    func joinString(#strings: String...) -> String {
            return joinString(strings: strings)
        }
    println(joinString(strings : ["I", "am", "an", "array"]))
    println(joinString(strings : "I", "am", "an", "array"))
}
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Rachit
  • 814
  • 9
  • 19

2 Answers2

3

This looks like a Swift bug (or an undocumented restriction) to me. Function/method overloading works generally, even with array vs variadic parameters:

class MyClass {

    func foo(arg: Int) { println("An integer") }
    func foo(arg: Double) { println("A double") }

    func joinString(#strings: String[])  { println("An array") }
    func joinString(#strings: String...)  { println("Variadic parameters")}

    func test() {
        foo(2)
        foo(3.14)
        joinString(strings : ["I", "am", "an", "array"])
        joinString(strings : "I", "am", "an", "array")
    }
}

and produces the expected output:

An integer
A double
An array
Variadic parameters

But overloading does not work for nested functions:

class MyClass {

    func test() {

        func foo(arg: Int) { println("An integer") }
        func foo(arg: Double) { println("A double") }
        // error: definition conflicts with previous value

        func joinString(#strings: String[])  { println("An array") }
        func joinString(#strings: String...)  { println("Variadic parameters")}
        // error: definition conflicts with previous value

        func test() {
            foo(2)
            foo(3.14)
            joinString(strings : ["I", "am", "an", "array"])
            joinString(strings : "I", "am", "an", "array")
        }
    }
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
2

You can't overload in this way:

func joinString(#strings: String[]) -> String {
 ...
}
func joinString(#strings: String...) -> String {
    ...
}

The joinString functions actually have same signature. Both take Array but the signature of the variadic version causes the compiler to generate with Array using the passed arguments at the call site.

Bruce1q
  • 504
  • 4
  • 8
  • If we write code this way:- func joinString(#strings: Double) { ... } func joinString(#strings: String) { ... } Then also its gives same error – Rachit Jun 30 '14 at 08:36
  • 3
    Those functions have different signatures. One takes an array of strings as argument, and the other takes "variadic parameters". Only *inside* the function the variadic `String...` is made available as array `String[]`. – Martin R Jun 30 '14 at 09:30
  • I played with this quite a bit this morning and I think you are right. This is really either a compiler bug or a basic language limitation. a `func foo(bars:Array){}` cannot be overridden by `func foo(bar: Bar){}` – Bruce1q Jun 30 '14 at 19:15