2

This question led me to uncover this issue.

If I have two initializers with the same signature in the same class in different modules, how can I specify which one I want to call? To use the same example as in that question, say I declare a convenience initializer like this:

extension UIStoryboard {
    convenience init(name: String, bundle: NSBundle?) {
        // ... do custom things ...

        // This line causes infinite recursion. (How) Can I specify which 
        // initializer I want to call? 
        self.init(name: name, bundle: bundle) 
    }
}

I assume this would also be an issue with two extensions that add methods with the same signature to the same class.

Is there a way to resolve that issue? Or is this just a bug?

Community
  • 1
  • 1
ahruss
  • 2,070
  • 16
  • 21
  • The only bug is you choosing to have two methods with the same signature and name. At least change the `bundle` argument's external parameter name. – CodaFi Oct 31 '14 at 01:30
  • @CodaFi If it isn't a valid name, shouldn't it not compile? – ahruss Oct 31 '14 at 01:31
  • Not necessarily. Because you can't override initializers or declare non-convenience initializers in extensions, the compiler (rightly, I think) assumes you meant to call yourself because you're the only initializer in town that matches that signature. I just wonder why anyone would knowingly declare such an initializer. – CodaFi Oct 31 '14 at 01:41

1 Answers1

0

You cannot declare two extension methods with same signature. Try overriding the subscript method of Array and it won't let you. If you want to use another initializer that change the signature and have the users of the API call that other initializer.

Encore PTL
  • 8,084
  • 10
  • 43
  • 78
  • There is no compile-time error in the code in my question. So it seems to me that it must be the case that either it is a legal thing to do and there is a way to make it work properly, or it is a bug that it's not an error. – ahruss Nov 22 '14 at 20:36
  • Could also be a bug in the compiler if it allows two different modules to define an extension method with same signature. – Encore PTL Nov 23 '14 at 01:35