Swift Programming Language has this to say about access control for extension:
You can extend a class, structure, or enumeration in any access context in which the class, structure, or enumeration is available. Any type members added in an extension have the same default access level as type members declared in the original type being extended. If you extend a public or internal type, any new type members you add will have a default access level of internal. If you extend a private type, any new type members you add will have a default access level of private.
Alternatively, you can mark an extension with an explicit access level modifier (for example, private extension) to set a new default access level for all members defined within the extension. This new default can still be overridden within the extension for individual type members.
I don't fully understand the above statement. Is it saying the following:
public struct Test { }
extension Test {
// 1. This will be default to internal because Test is public?
var prop: String { return "" }
}
public extension Test {
// 2. This will have access level of public because extension is marked public?
var prop2: String { return "" }
extension Test {
// 3. Is this the same as the above public extension example?
public var prop2: String { return "" }
}
Internal is the default access control in Swift world. Those internal classes can be accessed within the same module freely. Developer can even subclass it and override the inside properties and functions.
Three different access levels are provided by Swift 4 language. They are Public, Internal and Private access. Enables entities to be processed with in any source file from their defining module, a source file from another module that imports the defining module.
In Swift 4, an extension can reach across to a private declaration. This only applies to extensions that live in the same source file as the private declaration. In other words, an entity that is declared private is accessible from within any extensions for that type within the same source file.
Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used.
Your understanding is almost correct.
A more interesting way to put your scenario 3 would be
extension Test {
// The extension has access control internal, because of what the docs say:
//
// > If you extend a public or internal type, any new type members you add
// > will have a default access level of internal.
//
// Despite the extension being internal, this member is private because it's
// declared explicitly here.
private var prop2: String { return "" }
}
as well as
internal extension Test {
// The compiler will give a waning here, why would you define something public
// in an internal extension?
public var prop2: String { return "" }
}
Also you might find interesting that if your class, struct or enum is internal
, you will not be able to define a public
extension. The same goes for a private
class, struct or enum, for which you cannot define a public
or internal
extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With