3 Nuances of Swift Extensions

How often do we take an initial cursory look at some documentation, shake our heads and say, “Ok, sure! Got it!”, and then some time later get to the actual usage of that perceived understanding only to find out, “Woah – this is behaving differently than I expected! I wonder if the documentation says anything about this?!”

A few discussions I’ve had recently have prompted me to question what I thought I knew about Swift extensions. I have read documentation about extensions and I thought I understood them pretty thoroughly. However, these conversations, along with some experimentation done on my own revealed a few nuances that I didn’t pick up on before.

Update: Almost immediately after publishing this article, the Swift community chimed in and helped me figure out my fundamental hiccup which prompted the aforementioned experimentation in the first place. I’ve written a follow-up article called “Clarifying Swift Access Control”, describing that misunderstanding. I recommend giving that one a read to avoid making the same mistake I did!

Three nuances of extensions

In particular, the following three nuances challenged what I thought I knew about Swift extensions:

  1. The visibility Swift extensions have into the Type they’re extending. Can they see things marked private, for example?
  2. How that visibility is affected by where the extension is defined. If I have the source for a Type that I’m writing an extension for, does defining it within that same source file vs defining it in a separate file affect what it can “see”?
  3. The default access modifiers of the extension’s “members” and how specifying them or not specifying them affect what an extension exposes as public API for the Type it’s extending.

Before I begin, suppose that I have a public struct called Person. It has some private properties, name, gender, and age. An enum encapsulates the idea of Gender. The struct looks something like this:

Now, suppose that I wanted to extend Person and inspect the three nuances about the extension’s capabilities and behaviors that I introduced at the beginning of this article…

Extensions and visibility into extended Type

When I introduced that first nuance about visibility into the extended Type, I asked the question, “Can they see things marked private?” The answer surprised me at first: Yes…they can.

However, here’s where the second nuance comes in: It absolutely does matter where the extension is defined.

Defined within same file

Extensions defined within the same file as the Type they’re extending have access to private members of that Type.

For example, defining an extension to Person within Person.swift allows the extension to access private properties and functions! Who knew?!

“What?? Why?”, I thought to myself…

My reasoning as to why extensions defined within the same file behave this way is because when it comes down to it, I could have just written the extension’s implementation as part of the Type itself and it would have had the same effect.

I’m in the source file of the Type I’m “extending”. So whether I write the additional functionality as an extension for the Type, or just define what would have been in the extension inside the Type, itself, the net effect is the same.

Therefore, the compiler essentially says, “I see this extension being defined, but there’s really no point. It’s in the same file that the Type is defined in… so the developer could have just written all this code within the Type itself… so I’ll let him/her refer to private code blocks.”

Update: My reasoning above reveals that I truly didn’t have an understanding of Swift access control. I recommend giving my followup article titled “Clarifying Swift Access Control” a read for more details!

Defined in a separate file

Moving the extension definition into a separate file, however, causes the extension to lose that visibility into the Type it’s extending.

Following the inverse of my previous reasoning about private visibility when the extension is defined within the same file, this behavior actually makes sense to me.

Most of the time, you’d be writing an extension for Types that you don’t have the source to. In that scenario, extensions would have the same visibility that any client of the Type’s exposed API would have, namely, the things marked public.


Default extension access control

The final nuance also yielded some semi-surprising results for me. Apple’s documentation says it, but until I experimented and saw it in action, I didn’t catch the nuance around the default access control modifiers applied to extensions.

Default access when no explicit access modifiers specified

In short, when you declare an extension but specify no explicit access modifiers (ie, you just use the default), the extension’s default access level depends on the access level of the Type it’s extending.

  • If the Type is public or internal, the extension’s implementation “members” will be internal by default. The “surprise” for me I think is that extensions for public Types have internal members by default, unless you specify otherwise.
  • If the Type is private, the extension’s implementation “members” will be private by default.

Here’s what the extension looks like if we analyze it from the perspective of using no explicitly declared access modifiers (note that to gain access to private properties and functions, I’m declaring the extension within Person.swift):

Using the default access modifiers as shown in the code snippet above exposes access to the extension’s new API to instances within the same module. However, it does not expose additional public API for the Type it’s extending to a client of that Type that’s in another module (for example, the unit test target, which is another Swift module).

Same Module
Extended API within the same module

Different Module (test target)
Extended API in a different module

For some reason, I had it in my head that if a Type that I’m extending is public, the extension’s members would default to public. I don’t know why I thought that, but thankfully my experimentation cleared up!

Default access when using default extension declaration, but specify public for implementation

Adding public access control modifiers to the extension implementation’s members makes those members visible everywhere (that is, both within the same module, and within the test target).

The location of the extension’s declaration, be it within the same source file as the Type it’s extending, or in a separate source file, does not matter in terms of what the extension exposes when adding public members… But only extensions declared within the same source file as the Type it’s extending can see private members of that Type, as we discovered previously.

Extensions declared in same and separate source files
Public extension members defined within the same source file, and in separate source files...

Public extension members visible in different module (test target)
Regardless of where the extension is declared, all public members are visible to  other modules (such as the test target).

But notice that on the line where I’ve written extension Person { ... } that I haven’t specified an access control modifier for the extension, itself. I’ve only added public to its members. Even still, the new functions are visible to the test target which is a different module.

In other words, there’s no need to write public extension Person { ... }. Since Person is public, the extension just uses the Type’s access level for its own declaration.

Wrapping up

The three nuances about Swift extensions that were analyzed here were “surprising” enough to me to warrant some experimentation. My hope is that the analysis that was done will help clear up these subtleties for others who are struggling with understanding how Swift extensions behave!

  • Pingback: Swift Reactions « Under The Bridge()

  • dcompiled

    Hey Andrew – thanks for walking through this! Just a note though, the “private” access modifier _means_ “visible to anyone in the same file only” in Swift. That’s not unique or different for extensions. So I could write a helper class, subclass, etc. in the same file as a class with private members and see all of them. Or write a subclass in a separate file and see none of them. Extensions follow these same rules and shouldn’t be different in this regard.

    • Andrew Bancroft

      This is absolutely great – thank you for pointing this out. Herein lay my fundamental misunderstanding in this whole thing.

      I should have paid more attention to the Swift blog when they released access control modifiers in XCode 6 Beta 4 (https://developer.apple.com/swift/blog/?id=5). I think I saw “public”, “private”, and “internal”, and then went “Yeah yeah, got that”. Nope. I didn’t! :]

  • Pingback: Swift Crash Course |()

  • To clarify for anyone that finds this post and is searching with Swift 3 in mind… private is now private within the extension, even within the file.. If you want the old behavior that is described (where a method in an extension within the file of the Type _can_ be accessed by the Type), use fileprivate instead.

    • Thanks for commenting on this w/ respect to Swift 3. Saved me lot of time investigating this some issue I was having.

    • Gaurav

      I came here to mention the same thing. Good you’ve already documented that.
      Author: Very nice blog BTW.

    • I’ve gotta put a header at the top of this to help folks know this isn’t current for Swift 3. Honestly, one forgets what one has written over time. :]

  • Juan José Ramírez

    It would be appreciated to have and updated version for swift 3 🙂