Private Property Mix-Match in Swift Extension

Imagine you have a type (enum, struct, class, it doesn’t matter) like so:

struct SomeType {}

Then you add an extension to it with an internal var foo and a private var bar where foo just returns bar and bar returns the string “foo”:


Now, when we call SomeType().foo, we get “foo”, right? Right.

SomeType().foo prints “foo”

Then you add another extension to it with another internal var baz and another private var bar where similarly baz returns bar and bar returns the string “baz”:


Now, when we call SomeType().baz, we should get “baz”, right? Wrong.

SomeType().baz prints “foo”.

Weird right? What’s going on here?

Let’s try something else. Let’s remove bar from Extension II and see what happens:


Interestingly, we get the following error:

'bar' is inaccessible due to 'private' protection level

This is really weird because on the one hand, Extension II’s bar prints “foo”, but when it doesnt have a bar, the compiler throws an error. Weird, right? Why is that? What’s going on here?. This same behavior is present with instance and static properties and methods on enums (static only), structs, and classes.

This also works where the initial bar is in the original type definition and not in an extension:

Thing().foo and Thing.baz both print “foo”

Is this a bug? If not, why is this the correct behavior? Please let me know @garricn

Interestingly too, if you try this in the IBM Swift Sandbox, it crashes!

UPDATE (about 10 minutes later 🙂 Its a known bug!