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!
I think the Swift team should tell us which is better.


(Jens Alfke) #2

If one were better than the other, they wouldn’t both exist...

It depends on whether you need to use those entities in other source files in the same module.
If you don’t, use fileprivate. If you do, use private.

—Jens

···

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

I think the Swift team should tell us which is better.


(Marco S Hyman) #3

private is *more* restrictive than fileprivate. If you need to use the entities in other source files in the same module you want internal, not private.

···

On Sep 28, 2016, at 8:04 AM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

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

I think the Swift team should tell us which is better.

If one were better than the other, they wouldn’t both exist...

It depends on whether you need to use those entities in other source files in the same module.
If you don’t, use fileprivate. If you do, use private.


#4

To answer the original question: at file scope the access modifiers
`private` and `fileprivate` have exactly the same effect. It does not
matter which of them you use there.

Nevin

···

On Wed, Sep 28, 2016 at 12:14 PM, Marco S Hyman via swift-users < swift-users@swift.org> wrote:

> On Sep 28, 2016, at 8:04 AM, Jens Alfke via swift-users < > swift-users@swift.org> wrote:
>
>
>> On Sep 28, 2016, at 12:16 AM, Cao Jiannan via swift-users < > swift-users@swift.org> wrote:
>>
>> I think the Swift team should tell us which is better.
>
> If one were better than the other, they wouldn’t both exist...
>
> It depends on whether you need to use those entities in other source
files in the same module.
> If you don’t, use fileprivate. If you do, use private.

private is *more* restrictive than fileprivate. If you need to use the
entities in other source files in the same module you want internal, not
private.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Quinn “The Eskimo!”) #5

Right, but that doesn’t tell you which one you /should/ use. Personally I’d probably go for `fileprivate` because it seems more explicit to me, but I’d be interested in hearing other opinions.

Share and Enjoy

···

On 28 Sep 2016, at 17:18, Nevin Brackett-Rozinsky via swift-users <swift-users@swift.org> wrote:

To answer the original question: at file scope the access modifiers `private` and `fileprivate` have exactly the same effect. It does not matter which of them you use there.

--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


(Michael Ilseman) #6

All access control modifiers have a degenerate case (roughly) equivalent to another one, and the typical wisdom for which to choose is to analyze your intent and perhaps future intent.

“fileprivate” and “private” are identical at global scope.
“internal” and “fileprivate” are identical in single-file modules.
“public” and “internal” are (well, roughly…) equivalent if you only have a single module.

···

On Sep 29, 2016, at 1:30 AM, Quinn The Eskimo! via swift-users <swift-users@swift.org> wrote:

On 28 Sep 2016, at 17:18, Nevin Brackett-Rozinsky via swift-users <swift-users@swift.org> wrote:

To answer the original question: at file scope the access modifiers `private` and `fileprivate` have exactly the same effect. It does not matter which of them you use there.

Right, but that doesn’t tell you which one you /should/ use. Personally I’d probably go for `fileprivate` because it seems more explicit to me, but I’d be interested in hearing other opinions.

Share and Enjoy
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

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