0

I have checking out some code and I came across code that is marked as open for a variable and it is a stored property. What kind of use does open have over public in this case? This is some made up code:

open class God {
    open var hasSuperPowers = true
}

class HalfGod: God {
    override var hasSuperPowers = false
}

This does not compile: Cannot override with a stored property 'hasSuperPowers'. This is because the variable is stored.

My question is therefore: What difference does it make when marking my stored property either open or public? Can I do something with an open stored property, which I can not do with a public stored property?

I would expect xCode would give me warning that marking hasSuperPowers as open would have no effect and would falsely imply to other people that they can override this variable. Ofcourse, this only applies when someone can confirm me that open or public does not make any difference.

Edit: I got now three people showing me the difference between open and var. Does my question even get read? Again:

What difference does it make when marking my stored property either open or public? Again: STORED PROPERTY

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • 1
    @Sh_Khan can you point out the sentence that would help me finding the answer to my question in your provided link? I know the difference between `open` and `public`. – J. Doe Mar 08 '19 at 22:45
  • I think I have answered exactly your question. You can override stored property in another module only if it's public. I have shown what does it mean to override a stored property. – Sulthan Mar 08 '19 at 23:06
  • @Sulthan 'You can override stored property in another module only if it's public' What? Clearly we can not override stored properties. And only if it is public? I don't understand. – J. Doe Mar 08 '19 at 23:07
  • 1
    Clearly you *can* override stored properties. You cannot override them *with another stored property*. You can override a stored property only with a computed property. – Sulthan Mar 08 '19 at 23:08
  • @Sulthan Wow yes you are right :/ I feel dumb now – J. Doe Mar 08 '19 at 23:11

2 Answers2

1

You can override a property but not with a stored property. When overriding properties, you have to use a computed property, e.g.:

A class with a stored property:

open class God {
   open var hasSuperPowers = true
}

The property can be overriden only with a computed property:

class HalfGod: God {
    override var hasSuperPowers: Bool {
        didSet {
            print("\(oldValue) -> \(hasSuperPowers)")
        }
    }
}

or

class HalfGod: God {
    var hasPowers: Bool = false

    override var hasSuperPowers: Bool {
        get {
           return hasPowers
        }
        set {
           hasPowers = newValue
        }
    }
}

You can override properties from other modules only if they are open. public properties cannot be overriden from other modules.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

From the link in the comment:

An open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.

A public class is accessible but not subclassable outside of the defining module. A public class member is accessible but not overridable outside of the defining module.

What difference does it make when marking my stored property either open or public?

  • If its marked public, then you cannot override the member outside the defining module.

Can I do something with an open stored property, which I can not do with a public stored property?

  • You can subclass it outside of the defining module.
L0uis
  • 703
  • 5
  • 8