[Proposal] 'selfprivate' Access Control - allowing access in extension within '`self`'

There are quite a few complexities when using modules in small teams / projects:

  • quote heavy setup.
  • extra noise due to extra import statements.
  • app may accidentally have the same symbol name as in the module - compiler won't warn.
  • extra noise for "Module." prefixing in case compiler can tell there's a conflict.
  • unless when compiler doesn't (at which time "Reset Package Caches" Xcode's awkward workaround comes handy).
  • this one is quite bad: struct's autogenerated initializers are internal → hence can't be used from outside → lot's of boilerplate to add in the module.
  • modules or not - using file boundaries for access control might be in conflict with other rules in the team about where to put file boundaries.

So when it comes to a situation when the new code needs to use something private / fileprivate, and there's a choice of:

  1. "I'll keep this thing private / fileprivate and add my code into the same file".
  2. "I'll just make this thing internal instead of private / fileprivate".
  3. "I'll introduce modules to the app that previously didn't use modules, and make this thing internal to that module".

then modules are literally the last thing I'd like to do, and typically I'd resort to either 1 or 2 -- and it's always with great dissatisfaction → hence we have this topic surface every now and then, asking for a more lightweight way of a better access control rules compared to what we have today.

Partial list of related proposals and discussions over the years.

Nov 2016 https://forums.swift.org/t/swift-evolution-proposal-introduce-typeprivate-access-control-level/

Dec 2016 https://forums.swift.org/t/any-consideration-for-directoryprivate-as-a-compliment-to-fileprivate/

Feb 2017 https://forums.swift.org/t/discussion-final-lazy-fileprivate-modifiers/

Feb 2017 https://forums.swift.org/t/a-comprehensive-rethink-of-access-levels-in-swift/

Apr 2017 https://forums.swift.org/t/type-based-private-access-within-a-file/

Oct 2020 https://forums.swift.org/t/implement-alvarez-monteiros-typeprivate-possibly-with-one-or-two-extra-restrictions/

Dec 2021 https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/

Dec 2021 https://forums.swift.org/t/calling-private-methods-from-extensions-in-separate-files/

June 2022 https://forums.swift.org/t/introduce-type-private-access-level/

Aug 2022 https://forums.swift.org/t/proposal-selfprivate-access-control-allowing-access-in-extension-within-self/

2 Likes