private vs. fileprivate on global declaration in Swift3?


(frogcjn) #1

Should I use private or fileprivate to declare global variables/consts in Swift 3? e.g.

fileprivate let a = 1
fileprivate class SomeClass {
    fileprivate b = 0
}

or

private let a = 1
private class SomeClass {
    fileprivate b = 0
}

Thanks!

If they are identity, then I think the Swift team should tell us which is better.


(Dave Reed) #2

They aren't identical. Here's a good explanation:

http://useyourloaf.com/blog/swift-3-access-controls/

HTH,
Dave Reed

···

On Sep 28, 2016, at 3:22 AM, Cao Jiannan via swift-users <swift-users@swift.org> wrote:

Should I use private or fileprivate to declare global variables/consts in Swift 3? e.g.

fileprivate let a = 1
fileprivate class SomeClass {
    fileprivate b = 0
}

or

private let a = 1
private class SomeClass {
    fileprivate b = 0
}

Thanks!

If they are identity, then I think the Swift team should tell us which is better.


(Fritz Anderson) #3

Useful, but the OP asks about “global” symbols, which the examples show means “top-level.” The two have identical scope, and structures within the file are nested in the top level; they have access to its private symbols. (Verified in Xcode 8.)

If you’re collaborating with people who care, follow their coding standard. Otherwise it’s a matter of preference. Strive for the serenity to know that not everything needs a “best practice.”

(Hold a meeting. Write “SQL” on a whiteboard, presentation slide, or poster. Ask each person in the room to pronounce it. Be indifferent.)

Me, I’d go with “private,” just because it’s an English word. I can see an argument for using “fileprivate” to avoid precisely this kind of confusion.

  — F

···

On 28 Sep 2016, at 9:29 AM, Dave Reed via swift-users <swift-users@swift.org> wrote:

They aren't identical. Here's a good explanation:

http://useyourloaf.com/blog/swift-3-access-controls/


Swift3 - A declaration cannot be both 'final' and 'dynamic'
(Clément NONN) #4

fileprivate and private aren’t the same thing :

try this on a playground :
class Foo {
  fileprivate let test = "test"
  private let test2 = "test2"
}

let foo = Foo()
foo.test
foo.test2

You can see that foo.test work because his scope is the file whereas test2 doesn’t compile :
error: 'test2' is inaccessible due to 'private' protection level
private is restricted to its class
If you only have one class or struct and no other var or let in the global scope of the file, fileprivate do the same thing than private.

Best,

Clément Nonn

···

Le 28 sept. 2016 à 18:52, Fritz Anderson via swift-users <swift-users@swift.org> a écrit :

On 28 Sep 2016, at 9:29 AM, Dave Reed via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

They aren't identical. Here's a good explanation:

http://useyourloaf.com/blog/swift-3-access-controls/

Useful, but the OP asks about “global” symbols, which the examples show means “top-level.” The two have identical scope, and structures within the file are nested in the top level; they have access to its private symbols. (Verified in Xcode 8.)

If you’re collaborating with people who care, follow their coding standard. Otherwise it’s a matter of preference. Strive for the serenity to know that not everything needs a “best practice.”

(Hold a meeting. Write “SQL” on a whiteboard, presentation slide, or poster. Ask each person in the room to pronounce it. Be indifferent.)

Me, I’d go with “private,” just because it’s an English word. I can see an argument for using “fileprivate” to avoid precisely this kind of confusion.

  — F

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users