Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Control for Swift Extensions

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 "" }
}
like image 600
Boon Avatar asked Nov 25 '15 20:11

Boon


People also ask

What is default access control in Swift?

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.

What are the different access levels in Swift?

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.

Can we access private in extension in Swift?

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.

What are the access control in Swift?

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.


1 Answers

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.

like image 197
mokagio Avatar answered Nov 16 '22 17:11

mokagio