Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'open' keyword in Swift?

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?

like image 415
Clashsoft Avatar asked Aug 14 '16 22:08

Clashsoft


People also ask

What is open keyword in Swift?

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.

What is the difference between open and public keywords in Swift?

Open — This is where you can access all data members and member functions within the same module(target) and outside of it. You can subclass or override outside the module(target). Public — This is the same as open, the only difference is you can't subclass or override outside the module(target).

What is Swift keyword?

Swift keywords are the words reserved for a purpose. They cannot be used for variable names constants or any other identifiers. There are four types of keywords in Swift based on the location of their usage in a swift program. Keywords in Declarations. Keywords in Statements.

What is the difference between open and public and Fileprivate and private?

Open - same as public, only difference is you can subclass or override outside the module. Fileprivate - As the name say's, data members and member functions are accessible within the same file. Private - This is where you can have access within the scope of function body or class.


2 Answers

open is a new access level in Swift 3, introduced with the implementation of

  • SE-0117 Allow distinguishing between public access and public overridability

It is available with the Swift 3 snapshot from August 7, 2016, and with Xcode 8 beta 6.

In short:

  • 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.

So open is what public used to be in previous Swift releases and the access of public has been restricted. Or, as Chris Lattner puts it in SE-0177: Allow distinguishing between public access and public overridability:

“open” is now simply “more public than public”, providing a very simple and clean model.

In your example, open var hashValue is a property which is accessible and can be overridden in NSObject subclasses.

For more examples and details, have a look at SE-0117.

like image 99
Martin R Avatar answered Oct 01 '22 03:10

Martin R


Read open as

open for inheritance in other modules

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 except that something that is public is closed for inheritance in other modules.

From Apple Docs:

Open access applies only to classes and class members, and it differs from public access as follows:

  1. Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.

  2. Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.

  3. Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.

  4. Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.

like image 38
Mohammad Sadiq Avatar answered Oct 01 '22 03:10

Mohammad Sadiq