[swift-users] private vs. fileprivate on global declaration in Swift3?

Clément NONN clement.nonn at icloud.com
Thu Sep 29 02:51:59 CDT 2016


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 at swift.org> a écrit :
> 
> On 28 Sep 2016, at 9:29 AM, Dave Reed via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> They aren't identical. Here's a good explanation:
>> 
>> http://useyourloaf.com/blog/swift-3-access-controls/ <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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160929/2aed5d38/attachment.html>


More information about the swift-users mailing list