[swift-evolution] [Review] SE-0159: Fix Private Access Levels

Jonathan Hull jhull at gbis.com
Sat Mar 25 02:32:01 CDT 2017


Ok, I have been thinking all day if I can come up with of a design which would still provide scoped private (and keep file-based private) without causing the current confusion.  Here is the best I have so far.  I warn you it trades one weirdness for another, but at least the new weirdness is progressively disclosed, so it should be less problematic:

Swift 4
	• Depreciate ‘private’ with a warning that it will be removed in Swift 5

Swift 5
	• Remove ‘private’ keyword
	• Rename ‘fileprivate’ to ‘local’ with the understanding it now means local to the containing submodule
	• If no submodule is declared, the file is its own anonymous submodule
	• A group of files can declare themselves a submodule by each saying ‘module name’ with the same name somewhere in the file (only one declaration is allowed per file)
	• Within the file ‘module { }’ can declare an anonymous module (within the braces) nested inside the current module
	• For fun: Stored properties are allowed to be declared within extensions in a type’s containing submodule

That ‘module { }' part is the new weirdness, but it should cause less confusion overall.  A few points:
	• They could be nested as deep as you like
	• It needs to be anonymous so you aren’t tempted to connect it with other submodules (in other files)
	• You could recreate the current behavior of private by wrapping your type in a module block
	• …or you could wrap that block around a type and it’s friend
	• …or you could wrap only part of your type (crucial if you want both private and fileprivate behavior in the same type)
	• Internal, public, and open would retain their current meanings


Thoughts on this approach?  It isn’t my first choice, but it is much better than what we have now.  

It should let you do everything you do now and then some (but with the extra complexity tucked away for the most part).  I.e. If you don’t know what submodules are, the system appears structurally the same as Swift 2.  You can learn about submodules without learning about ‘module {}’.  In exchange, scoped private becomes more complex… but you also have finer control over the scope (it can cover only part of a large type or two friend types).

Thanks,
Jon


More information about the swift-evolution mailing list