3

The following code does not compile:

class object {
    class var myVar: String! {
        return "from class"
    }

    var myVar: String! {
        return "from instance"
    }
}

Error:

Invalid redeclaration of 'myVar'

Is this not possible with Swift or am I doing it wrong? This is totally legal in Objective-C (as functions rather than variables).

1
  • 1
    If you think that is a good idea file a radar. (I don't, it can easily cause confusion and errors.) Commented Jul 24, 2014 at 14:31

1 Answer 1

3

Currently, you can't. If you really want to do this, you can file a Bug Report with Apple.

I would advise against doing this. It's really bad practice, simply because having two different variables named the exact same thing, the only difference being how they are accessed, is a terrible code smell. And it's confusing.

So file a radar if you want to, and then change your variable names. I doubt Apple will "fix" this.

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

2 Comments

Totally agreed, it's very terrible.
I agree having same name might not be a good styling, but I don't think we need paint over the fact that it's a bug, just check out how other languages do (even in Objective-C)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.