[swift-evolution] [Discussion] fileprivate vs. private(file)

Haravikk swift-evolution at haravikk.me
Mon Feb 20 08:27:02 CST 2017


> On 20 Feb 2017, at 13:30, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> On Feb 20, 2017, at 3:42 AM, Haravikk via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> The proposal I preferred was to use only the public and private keywords and use parameters to provide greater specificity, like so:
>> 
>> 	public			as it is now
>> 	private(module)	equivalent to internal, which is private to the current module/project
>> 	private(file)		equivalent to fileprivate
>> 	private(scope)		equivalent to current private
> 
> We considered this last year and decided it was a bad idea. The `private` keyword ends up just being surplusage—it just adds noise without adding meaning.

It may not add a lot of meaning as such, but personally I find it a lot cleaner; everything other than public is a form of privacy by module, file etc., so it makes a lot of sense to me to have them grouped in that way. The current keywords are also inconsistent; we have public, private, internal and fileprivate, only one of these is a conjoined keyword, and internal isn't any less a form of privacy than private and fileprivate.

I also disagree that it adds noise; to me I find the above easier to read, as private tells me that the property/method is being restricted in some way, and I can look at the parameters to find out more (if there are any). Bear in mind that most of the time developers will still only use public, private and nothing at all (the private(module) default), they only need to add the parameter when something more unusual is required.

In fact, it's one argument for the reduction in keywords, as in my experience I very rarely see anyone actually use internal, usually they just omit the keyword, so most of the time it is a waste to define it, and internal might be nice to have available as a variable name rather than reserved as a keyword that most people rarely, if ever, use. Part of my concern is that there are a number of proposals on the list at the moment that may seek to add new keywords, so trimming this down is useful. It's also a very easy change to migrate; fileprivate becomes private(file), internal becomes private(module).

I'd also argue that private(module) is clearer in meaning than internal, and private(scope) is much clearer in meaning that just private, though I'm currently undecided of which should be the default for private without parameters.

> it's something else to rename things all across the language for the sake of renaming them.

I think it's unfair to say it's just "for the sake of renaming them"; I feel it's a more consistent and more future proof scheme that cuts down on the number of keywords required, and I also find it easier to read.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170220/19d20294/attachment.html>


More information about the swift-evolution mailing list