[swift-evolution] [Proposal] Nested extensions

Xiaodi Wu xiaodi.wu at gmail.com
Sat Apr 15 10:33:36 CDT 2017

On Sat, Apr 15, 2017 at 10:23 AM, Tino Heth <2th at gmx.de> wrote:

> > I think you misunderstand. The rules of access modifiers do not
> currently work with nested extensions without modification. How do you
> intend to modify the rules?
> There are no nested extensions now, right? So there are also no rules for
> their access modifiers, if I'm not missing something fundamental (which
> might be the case — please elaborate on this if my assumptions are wrong).
> Afaik, the ideas you mentioned wanted to change how access modifiers of
> extensions work, whereas I don't want to change any of their rules.
> I even don't want to add any new rules:
> Just like today, an extension would introduce a separate scope, and the
> only meaning of its access modifier would be to act as the default for all
> elements of this extension.
> The single change for nested extensions compared to regular ones would be
> the treatment of private (which changes the default for members to
> fileprivate, so it's rather inconsistent anyways, and not a real new rule,
> but rather removal of an exception).
> This change wouldn't be necessary, but it would be inconvenient to
> manually mark the majority of its members as private.

I think you misunderstand how access modifiers currently work with
extensions. It is not permitted to use an access modifier inside an
extension that is higher than that of the extension itself. I pushed for
this rule not to be the case, in SE-0025, and in subsequent discussions, as
you saw. That was not accepted. Therefore:

private extension Foo {
  internal func bar() { } // this is a compiler error, because internal >
top-level private

It is not an exception to default members to `fileprivate` inside a
`private` extension. It is the only solution, but it only works for top
level extensions:

private extension Baz {
  fileprivate func boo() { } // this is allowed, because fileprivate ==
top-level private

There is no way to spell the access level of a nested private extension:

private struct S {
  private extension {
    func f() { } // what is the access level of `f`?

If `f` is private, then it is invisible outside the extension. But `f`
cannot be `fileprivate`, because `fileprivate` is more visible than the
private extension and is therefore not allowed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170415/db4e3d8a/attachment.html>

More information about the swift-evolution mailing list