[swift-evolution] private & fileprivate

David Hart david at hartbit.com
Fri Oct 7 11:13:21 CDT 2016

I started the topic, but I also believe like you that the fileprivate vs private(file) discussion has already been thoroughly discussed and nothing new has been brought up. That’s not what I want to discuss.

I instead want to share my experience using private and fileprivate since release. Here are my thoughts:

We should start with the premise that the proposal has added a substantial amount of complexity:
It has added an extra modifier and access level to learn.
It has complicated the access level rules with Inner types as mentioned in the Complications with private types section of the proposal.
I have seen many people (twitter, work, slack) be confused about the difference between private and fileprivate at the global level. The answer is none, which shows that both modifiers are not very orthogonal.
Since release, I saw people prefer one over the other, as a matter of style. They tend to always use fileprivate or always using private. In the latter case, functions and properties get clumped in the same class scope instead of be written through multiple extensions.
I have the impression that the motivations for the proposal are much less real in practice:
The first motivation stated is: "It is not clear whether the implementation details are meant to be completely hidden or can be shared with some related code without the danger of misusing the APIs marked as private.” I’ve found that to be fairly rare in practice because the implementation details only used to leak inside the same file, which greatly reduces the dangers.
The second motivation stated is: "It forces a one class per file structure, which is very limiting." First of all, this is partly false. I think it forces putting classes which share implementation details in the same file, which I don’t think is necessarily a bad thing.

To summarise, it seems that the confusion the proposal brought over semantics and style are not worth the limited benefits that it brought. I’d be tempted to backtrack the proposal and re-introduce private as a file scoped access-level and deprecate fileprivate.


> On 7 Oct 2016, at 17:21, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> While no topic is formally off the table, to revisit a topic requires fresh insight. `private(file)` was suggested at the time and rejected in favor of `fileprivate`, and we really don't need another rehash of how much each person likes one or the other.
> On Fri, Oct 7, 2016 at 09:02 Adriano Ferreira via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> +1
> I would also rather have:
> private(scope)
> private(file)
> private(module)
> etc…
> — A
>> On Oct 7, 2016, at 4:24 AM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> On 7 Oct 2016, at 07:39, David Hart via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> Hello community,
>>> From all the proposals which has gone into Swift 3, [SE-0025] Scoped Access Level is the only one I’m having second thoughts about. Before launching a discussion around it, I’m curious to know if it's worth discussing it or if the “ship has sailed”. As the plan is to allow future versions of Swift to break source-compatibility in certain rare scenarios, perhaps we have a chance to reconsider certain proposals?
>>> Regards,
>>> David.
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> What in particular don't you like about it?
>> Personally I still don't like the use of fileprivate as the keyword, I was very much in favour of a bracketed system like:
>> 	private(scope)		Current private (I think, it doesn't appear to be equivalent to protected in other languages anyway so I wouldn't call it type).
>> 	private(file)		Current fileprivate
>> 	private(module)	Current internal/default when omitted
>> 	public			Current public
>> I favour this because it groups all restrictive access levels under private (since they're all some form of private) with an optional modifier that's explicit about what it's for. Also, it would have scope to move things like final into a modifier too, so you might declare a method as public(final), or public(open) if that's implemented later and so-on. Just seems like a generally more flexible setup that also reduces the number of keywords required.
>> Some may feel it's noisy, but personally I don't see it as a problem as it always comes before the func/var/let keyword, generics and function name, so it's not like it's near anything where the (minor) noise reduces readability.
>> But yeah, having used the new fileprivate for a little while I just don't like it; it may partly come down to the fact that I use fileprivate a lot more than I use regular private. If we were to adopt the above scheme I would recommend that private(file) be the default for use of the plain private keyword, unless we gain the ability to specify private(type) (i.e- protected in most other languages), as private(scope) seems like it's the less common, at least in my experience.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161007/438b373d/attachment.html>

More information about the swift-evolution mailing list