Why should Swift introduce ‘fileExtensionPrivate’ access modifier?

Yeah, as per the title, you’re reading it surprisingly with a tiny smile on face 🙂!

Get Started

Yes, right now, there is a limit with all the existing modifiers. To understand the limit, let’s first talk about private, filePrivate and internal access modifiers.

Private access modifier:

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. In the below code, you can see that name is a private property of ViewController class. It is accessible in the ViewController class & also in the extension of ViewController in the same file, but it is not accessible in the other SecondViewController class in the same file.

filePrivate access modifier:

fileprivate access restricts the use of an entity to the source file level. As in the above code, name is private and it is not accessible in the SecondViewController class in the same source file. fileprivate access modifier overcome this problem. In the below code, you can see that name is declared as fileprivate and now it is accessible in the SecondViewController class within the same source file.

Internal access modifier (default access modifier):

Internal is default access level. Internal access modifier restricts the access within the module. If you define anything (class/struct/method/property etc) with internal type then it will be accessible within the same module in which target is defined. In the below code, country is a property with internal access modifier of ViewController class. It is accessible with in the same file in any extension, methods & class etc and it is accessible in other files also like SecondViewController.

Why required ‘fileExtensionPrivate’ access modifier?

Now lets talk about the limit of these access modifiers and why Swift introduce fileExtensionPrivate access modifier. In the below code, there are 3 properties name, city & country and their access modifiers are private, filePrivate & internal respectively. I create an extension of ViewController class in the other SecondviewController file. Then name & city are not accessible as per their access levels. But country is accessible as it is defined as internal level but problem is that it is also accessible by other classes also in the entire module. I can not make it controllable only for ViewController class in multiple files. As country is internal then it creates confusion to understand the use of this property. It indicates that it is declared internal to use in entire module but here it is declared internal to access only in extension in other file.

To overcome this problem Swift should introduce a new access modifers ‘fileExtensionPrivate’ to give the access level to its extensions in multiple files.

Summary

As Swift provides clean & readable statements with advanced & strong concepts like struct, enum, protocol, generic etc, It should provide more control on data access levels.

Thank You. Happy coding! 👍

Enjoy some more my previous articles!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store