The ObjectiveC.swift
file from the standard library contains the following few lines of code around line 228:
extension NSObject : Equatable, Hashable {
/// ...
open var hashValue: Int {
return hash
}
}
What does open var
mean in this context, or what is the open
keyword in general?
open come to play when dealing with multiple modules.
open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.
Except for subclass, an
open class func
is also accessible from anywhere in your project. Say you are defining an open class function in your Util class like thisThen in anywhere of your view controllers, you can simply access this method by calling
Read open as
I repeat open for inheritance in other modules. So an open class is open for subclassing in other modules that include the defining module. Open vars and functions are open for overriding in other modules. Its the least restrictive access level. It is as good as public access accept that something that is public is closed for inheritance in other modules.
From Apple Docs:
open
is a new access level in Swift 3, introduced with the implementation ofIt is available with the Swift 3 snapshot from August 7, 2016, and with Xcode 8 beta 6.
In short:
open
class is accessible and subclassable outside of the defining module. Anopen
class member is accessible and overridable outside of the defining module.public
class is accessible but not subclassable outside of the defining module. Apublic
class member is accessible but not overridable outside of the defining module.So
open
is whatpublic
used to be in previous Swift releases and the access ofpublic
has been restricted. Or, as Chris Lattner puts it in SE-0177: Allow distinguishing between public access and public overridability:In your example,
open var hashValue
is a property which is accessible and can be overridden inNSObject
subclasses.For more examples and details, have a look at SE-0117.
Open is an access level, was introduced to impose limitations on class inheritance on Swift.
This means that the open access level can only be applied to classes and class members.
In Classes
An open class can be subclassed in the module it is defined in and in modules that import the module in which the class is defined.
In Class members
The same applies to class members. An open method can be overridden by subclasses in the module it is defined in and in modules that import the module in which the method is defined.
THE NEED FOR THIS UPDATE
Some classes of libraries and frameworks are not designed to be subclassed and doing so may result in unexpected behavior. Native Apple library also won't allow overriding the same methods and classes,
So after this addition they will apply public and private access levels accordingly.
For more details have look at Apple Documentation on Access Control
open is only for another module for example: cocoa pods, or unit test, we can inherit or override