3

Here's a contrived example:

class AwesomeClass {            
   var answerToEverything: NSInteger = 42

   class func answerToEverything() -> NSInteger {
    return 42
  }
}

Per my Swift understanding, there should be no issue here: var answerToEverything and class func answerToEverything have different signatures: var answerToEverything applies to the instance and class func answerToEverything() to the class.

However, this gives a compiler error:

Invalid redeclaration of 'answerToEverything()

Why does an instance parameter and class function with same names give this error?

In case it matters, I'm using Xcode 7.3 (7D175).

Note: it makes sense why an instance method and instance parameter can't have the same name. The compiler couldn't distinguish them. However, this question is about a class method and an instance parameter. This is different from the proposed duplicate question.

7
  • @JAL: Thanks for the link. However, this question is somewhat different. Commented Apr 6, 2016 at 18:48
  • The other question asks about an instance method and an instance parameter. This makes sense for why it's a bad idea--> as answered, which one should be used? The compiler can't really distinguish them. However, this question is about a class method and instance parameter. AFAIK, the compiler should be able clear distinguish them. Commented Apr 6, 2016 at 18:48
  • AFAIK, the compiler should be able clear distinguish them. How? Why do think so? Commented Apr 6, 2016 at 18:51
  • 2
    I was wrong, it is a bit different from stackoverflow.com/questions/29016524/…. The real duplicate is stackoverflow.com/questions/24936426/… (as mentioned below). – Now someone else has to close as a duplicate! Commented Apr 6, 2016 at 19:02
  • @MartinR Yep, thanks. :-) Commented Apr 6, 2016 at 19:46

1 Answer 1

0

Identifiers have to be unique, it's not about what their functions are. The same would happen in the following example:

let ViewController: Int = 10

class ViewController: NSViewController {

}

The initial variable won't be flagged up by the compiler, but the class will be flagged as a duplicate.

This doesn't apply if you:

  1. define let ViewController: Int = 10 in one Module and in another..
  2. define class ViewController: NSViewController

The two two modules separate the identity spaces (namespaces in a way)

Sign up to request clarification or add additional context in comments.

8 Comments

This is different. In your example, ViewController in both cases is within the same scope --> the compiler can't distinguish them.
However, instance parameters and class methods are in different scopes... One applies to the class and the other to the instance.
This also appears to be a previous question: stackoverflow.com/questions/24936426/…
I you want to be able to have duplicate names, file a rdar with Apple, but I see no benefit to this, if anything it seems more of a hinderance.
Yes, the previous question makes this a duplicate. Thanks! I don't want to do this, but certain Foundation classes do this. Sigh, this effectively makes it impossible to subclass them in Swift. :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.