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

Haravikk swift-evolution at haravikk.me
Mon Feb 20 08:39:03 CST 2017


> On 20 Feb 2017, at 12:27, Ross O'Brien <narrativium+swift at gmail.com> wrote:
> 
> Orthogonal extensions, such as 'protected', multiply the problem. How do you restrict which access levels get 'type' access?

Well, I was really just mentioning it as a future possibility, I hadn't quite gotten as deciding exactly how type would be used. It's still a form of privacy so I expect it would be grouped under private(type), with the caveat that if the type is public then any private(type) methods and properties will be available to external sub-classes.

> Perhaps, by default:
> 
> These four are equivalent:
> var someValue: Int = 0
> private var someValue: Int = 0
> private(module) var someValue: Int = 0
> private(get: module) var someValue: Int = 0
> 
> 'set' defaults to 'get' level unless explicitly restricted further, e.g. these two are equivalent
> private(set: file) var someValue: Int = 0
> private(get: module, set: file) var someValue: Int = 0
> 
> Perhaps 'type' access is 'set' level unless explicitly exposed:
> private(get: file, set: scope, type: module)
> 
> ... It doesn't make sense to me that 'type' would be more restricted than 'set', but offhand I haven't figured out if 'type' can be more exposed than 'get'. If there are restrictions like this the compiler can complain that the access levels specified are inconsistent.
> 
> This still seems a bit clunky to me if you have a public and a private on the same line, but perhaps someone can suggest something for that.

This is what I had in mind; i.e- you don't have to parameterise private, you'd only do it if you want something other than the default.

As for something with both public and private modifiers (only really applies to properties I think?) I think it's fine to just declare each separately, as there may in future be more public parameters so it makes sense to have the separate grouping of a property's public and private components.

I'm not clear on your use of private(type:module), what do you see that as representing? Personally I'd just expect them to be used like private(module, type), i.e- the method/property is private but accessible by both module and (externally) by sub-classes.

>> On Mon, Feb 20, 2017 at 11:42 AM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> So discussion of the exact naming convention for access modifiers has been discussed multiple times, especially when fileprivate was being introduced. It's now been a while since fileprivate was added, so we've all had more time to actually work with it, so I wanted to raise the discussion again to see what people now think.
>> 
>> Specifically, during the debate over access modifiers I really didn't like the propose fileprivate, as I don't like conjoined lower-case key-"words" (to me a keyword should always be a single word, I still don't like associatetype or typealias either, and neither does macOS' auto-correct 😉). And to be honest my opinion hasn't changed; if anything I hate it even more.
>> 
>> 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
>> 
>> The actual parameters are up for debate, but I still feel this a lot clearer. I also generally prefer the parameterised version for several reasons:
>> 
>> Readability: I know not everyone will agree on this one, but I much prefer the separation to having conjoined keywords.
>> Fewer Keywords: Maybe I'm a keyword purist or something, but I just don't like having too many keywords in the language, especially four for what amounts to a single feature (access).
>> Extensibility: It is easier and cleaner to add more parameters in future. For example, private(type) could be an equivalent to protected access in other languages. It can also be used to more easily group external extension modifiers under public such as public(final), public(open) etc.
>> 
>> For setter-specific access it might look something like:
>> 
>> 	private(file:set) var someValue:Int = 0
>> 
>> I know it might not seem that important to some people, but this is one area of Swift's syntax that still bothers me, perhaps unreasonably so, but I don't get to pick what annoys me! I'd also seek to replace associatedtype and typealias (and any other conjoined keyword I've forgotten) but I haven't thought of any alternatives that I like for those yet.
>> 
>> Anyway, just wanted to discuss how others felt having used fileprivate and such for a while, and whether it's worth revisiting, or if I just have to live with it.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170220/096b13ee/attachment.html>


More information about the swift-evolution mailing list