2

In Swift 3, what exactly is the difference in access control between

private class Foo { }

and

fileprivate class Bar { }

assuming both are top-level classes, that is, they are placed directly into a file rather than nested in some other construct?

brendon-ai
  • 517
  • 3
  • 7
  • 20
  • Related: [private vs. fileprivate on declaring global variables/consts in Swift3?](http://stackoverflow.com/q/39739813/2976878) – there is no difference in the case you propose. – Hamish Mar 27 '17 at 19:24
  • 1
    What about with implicitly-scoped variables inside the class? Do either of these affect their scopes? According to the docs, it should, but according to real-world testing, it doesn't. In both cases they are fileprivate. From the docs: If you define a type’s access level as private or file private, the default access level of its members will also be private or file private. – Mark A. Donohoe Apr 19 '17 at 18:19

1 Answers1

4

There is no difference in your case. The only time that fileprivate differs from private is inside a class, struct, or protocol.

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45
  • 3
    That's strange. I wonder why they just don't allow `private` on top-level entities. – brendon-ai Mar 27 '17 at 20:37
  • 4
    @Ethcad There are other degenerate cases for access control levels, see [this post](https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160926/003474.html) from the mailing list discussion on the matter. Making a special exception for the particular case of top-level entities is unnecessary IMO. – Hamish Mar 27 '17 at 20:39