Here's something that I saved for myself as a gist long time ago.
Having all these new access modifier keywords adds so much cognitive load. Why can't we simplify things by only having private
, internal
, public
and open
access modifiers?
Anything else would be achieved through access modifier limiters in a hypothetical form of a suffix (or a prefix).
Current | New |
---|---|
private | private(file) |
- | private |
fileprivate | internal(file) |
internal | internal |
public | public |
open | open |
- Current
private
would be re-branded asprivate(file)
orprivate
with(file)
limiter. - The new
private
will be type-private by default and does not require an explicittype
limiter, like currentinternal
and higher access modifiers already do today. - Current
fileprivate
would be rebranded asinternal(file)
which clearly describes the visibility of the modifier that it has today. - [Debatable]: Top-level usage of
private
or a limitedprivate(file)
could be banned in favor ofinternal(file)
, or at least non limitedprivate
has to be banned. - Last but not least, we could also try and see if
closed protocols
can be patched in shift as well.-
public protocol
would disallow the user of the framework to conform to the protocol, similar to howpublic class
disallows inheritance. -
open protocol
would act like currentpublic protocol
and allow conformances again.
This would allow library vendors to expose some implementation detail protocols to the public without worrying that the library user would conform some types to it, hence these will be closed to the library.
-
A phased migration can be purely mechanical.
- Phase 1: Rename
private
toprivate(file)
,fileprivate
tointernal(file)
,public protocol
toopen protocol
and disallow pureprivate
access modifier as well aspublic protocol
in that transition (or warn about the upcoming changes) - Phase 2: Introduce new
private
andpublic protocol
.