[swift-evolution] Type-based ‘private’ access within a file
svabox at gmail.com
Tue Apr 4 05:35:19 CDT 2017
On 04.04.2017 5:03, BJ Homer via swift-evolution wrote:
> This type-and-file-based proposal addresses most of the *pragmatic* issues
> people run into when writing Swift, but I agree with other comments that
> it's a difficult mental model.
> It sounds like most everyone likes the idea of renaming "private" ->
> "scoped" and "fileprivate" -> "private", but the code churn is considered
> too large for Swift 4. What about the following alternative, which is
> similar to SE-0159 but avoids the code churn:
> - Revert the meaning of "private" to the Swift 2 meaning, as in SE-0159.
> - Make "fileprivate" an alias for "private", as in SE-0159
> - Migrator converts "fileprivate" -> "private", as in SE-0159
> - Introduce "scoped", but perform no automatic migration for it.
FWIW Currently I believe this is the best alternative/compromise we can
have. I think anyone who defer the current scoped 'private' is OK to
manually change it to 'scoped' in new version of Swift.
And those who support SE-0159 will have Swift 2.0 'private'. As for
fileprivate probably it should be marked as deprecated so we can later
remove it from language.
Actually I'm very surprised :-( of core team decision and ultimate reaction
on community's loud request to rename fileprivate->private and current
private->scoped to achieve the both targets(to have Swift 2 'private'
instead of 'fileprivate' and keep the usefull current 'private' access
level). In addition it was said that it is the last chance to change
anything in access modifiers. IMO this BJ Homer's solution/compromise is
the way we *should* go given that this is *the last chance* to improve the
situation with access modifiers.
As was said by core team, there will be no *any* reconsideration on access
levels/keywords after this, even for submodules etc. And core team even did
not discussed a lot features like submodules in the light of access
modifiers(they said this).
I want to ask/recommend anyone who want to improve the situation with
access modifiers - reply to BJ Homer's message with support for it, so core
team can see the consolidated opinion they can take into account.
> The discussion around SE-0159 has shown that there are indeed important use
> cases for scoped access control. However, most instances of "private" in
> the wild are probably just due to its position as a "soft default", and
> don't need any migration. Developers who are relying on scoped access
> control are likely to be aware of locations where it is important, and
> could manually rewrite "private" to "scoped" for those sites. (For users
> who want to perform a full migration of "private" -> "scoped", perhaps a
> manual migration script could be provided.)
> It's somewhat unfortunate to require manual migration to "scoped" for code
> that cares about scoped access, but I suggest that those use cases are rare
> and the developers are generally aware of such cases. This proposal prefers
> to limit the code churn instead, while getting rid of the "fileprivate"
> wart on the language. Most users would be able to migrate to Swift 4 with
> only the amount of migration already proposed in SE-0159.
>> On Apr 3, 2017, at 8:34 PM, Douglas Gregor via swift-evolution
>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Hello Swift Community,
>> In rejecting SE-0159
>> the core team described a potential direction we would like to
>> investigate for “private” access control that admits a limited form of
>> type-based access control within files. The core team is seeking some
>> discussion here and a motivated volunteer to put together a proposal
>> along these lines for review in the Swift 4 time-frame (i.e., very soon).
>> To be clear, the core team it’s sure this is the right direction to go…
>> but it appears promising and we would *love* to be able to settle the
>> access-control issue.
>> The design, specifically, is that a “private” member declared within a
>> type “X” or an extension thereof would be accessible from:
>> * An extension of “X” in the same file
>> * The definition of “X”, if it occurs in the same file
>> * A nested type (or extension thereof) of one of the above that occurs in
>> the same file
>> This design has a number of apparent benefits:
>> + “private” becomes the right default for “less than whole module”
>> visibility, and aligns well with Swift coding style that divides a type’s
>> definition into a number of extensions.
>> + “fileprivate” remains for existing use cases, but now it’s use it more
>> rare, which has several advantages:
>> + It fits well with the "progressive disclosure” philosophy behind Swift:
>> you can use public/internal/private for a while before encountering and
>> having to learn about “fileprivate” (note: we thought this was going to
>> be true of SE-0025
>> but we were clearly wrong)
>> + When “fileprivate” occurs, it means there’s some interesting coupling
>> between different types in the same file. That makes fileprivate a useful
>> alert to the reader rather than, potentially, something that we routinely
>> use and overlook so that we can separate implementations into extensions.
>> + “private” is more closely aligned with other programming languages that
>> use type-based access control, which can help programmers just coming to
>> Swift. When they reach for “private”, they’re likely to get something
>> similar to what they expect—with a little Swift twist due to Swift’s
>> heavy use of extensions.
>> + Loosening the access restrictions on “private” is unlikely to break
>> existing code.
>> There are likely some drawbacks:
>> - Developers using patterns that depend on the existing lexically-scoped
>> access control of “private” may find this new interpretation of “private”
>> to be insufficiently strict
>> - Swift’s access control would go from “entirely lexical” to “partly
>> lexical and partly type-based”, which can be viewed as being more complicated
>> Thoughts? Volunteer?
>> - Doug
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> swift-evolution mailing list
> swift-evolution at swift.org
More information about the swift-evolution