Hello Swift Community,
I'd like to pitch a new access control keyword: folderprivate
.
Introduction
folderprivate
would restrict access to properties, methods, and objects to the same folder or inner folders, filling a gap between fileprivate
and internal
.
Motivation
As Swift projects grow, especially with SwiftUI, files can become unwieldy. Extracting views and properties into separate files improves maintainability, but current access levels are either too restrictive or too permissive. folderprivate
aims to solve this by promoting modular design and better organization.
Proposed Solution
Introduce the folderprivate
keyword to restrict access to the same folder and its inner folders, enhancing code encapsulation and organization.
Detailed Design
- Syntax:
folderprivate
can be applied to classes, structs, enums, properties, and methods.folderprivate struct FolderPrivateStruct { folderprivate var folderPrivateProperty: String = "Accessible within the folder" folderprivate func folderPrivateMethod() { print("This method is private within the folder") } }
- Semantics:
- Folder-Level Access: Members marked with
folderprivate
can only be accessed within the same folder or any subfolders. - Inheritance: Subclasses in the same folder or subfolders can access
folderprivate
members of their superclass.
- Folder-Level Access: Members marked with
- Compiler Enforcement: The compiler will enforce this by checking the directory structure.
- Integration with Build Systems: Build systems need to recognize and respect folder boundaries for
folderprivate
access control.
Examples
Consider a scenario where a main view has multiple child views that should not be exposed outside of the folder. Only the main view should be accessible outside the folder.
// File: /Views/MainView/MainView.swift
struct MainView: View {
var body: some View {
VStack {
HeaderView()
ContentView()
FooterView()
}
}
}
// File: /Views/MainView/HeaderView.swift
folderprivate struct HeaderView: View {
var body: some View {
Text("Header")
}
}
// File: /Views/MainView/ContentView.swift
folderprivate struct ContentView: View {
var body: some View {
Text("Content")
}
}
// File: /Views/MainView/FooterView.swift
folderprivate struct FooterView: View {
var body: some View {
Text("Footer")
}
}
In this example, HeaderView
, ContentView
, and FooterView
are only accessible within the /Views/MainView
folder, ensuring encapsulation and preventing unintended usage outside this folder.
Impact on Existing Code
This is purely additive and does not impact existing code. Current access control levels remain unchanged and fully functional.
Alternatives Considered
- Folder-Level Modules: Significant restructuring and build system changes.
- File-level Extensions: Over-segmentation and does not provide the desired encapsulation within a folder.
Future Directions
If well-received, further refinements could include more granular access control keywords or hierarchical access controls.
Conclusion
The folderprivate
keyword enhances Swift by providing a new level of encapsulation, aligning with the organizational needs of modern Swift projects. It promotes more modular, maintainable, and readable codebases.
Looking forward to your feedback and thoughts!