Search code examples
swiftaccess-controlswift-extensions

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 "" }
}

Solution

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