[swift-evolution] Analysis of existing scopes

Joanna Carter joanna at carterconsulting.org.uk
Wed Feb 22 11:07:19 CST 2017


> Le 22 févr. 2017 à 17:53, BJ Homer <bjhomer at gmail.com> a écrit :
> 
> My understanding is that “private” when written at file scope is exactly equivalent to “fileprivate”. That is, at the top level it’s tied to the file, not to the type’s scope. This means that the default access level within “private extension Foo {}” is actually *fileprivate*. This conflation of private and fileprivate is confusing.
> 
> Example: This compiles:
> 
> ---------------
> 
> class Blah {
>     
>     func start() {
>         self.test()
>     }
> }
> 
> private extension Blah {
>     func test() {
>         
>     }
> }
> 
> ---------------
> 
> While this does not:
> 
> ---------------
> 
> class Blah {
>     
>     func start() {
>         self.test()
>     }
> }
> 
> private extension Blah {
>     private func test() {  // Note the “private” here 
>         
>     }
> }

Indeed, that appears to be confusing but, as you more than likely found out, if you mark both the extension and its func as fileprivate, the problem goes away.

Nonetheless, with :

private extension Blah
{
  func test()
  {
    
  }
}

… the test() method is not visible outside of the file because the extension is private and, it would seem, that is enough to hide all of its code.

--
Joanna Carter
Carter Consulting



More information about the swift-evolution mailing list