[swift-evolution] Enhancing access levels without breaking changes
Tino Heth
2th at gmx.de
Sat Apr 8 07:03:42 CDT 2017
Imho there is a simple solution to reach the goals of SE-0169 without breaking compatibility:
Just allow extensions inside type declarations.
class MyVC: UIViewController {
private let numberOfSections = 0
extension: UITableViewDataSource {
// Skipping the class and assume we want to extend the surrounding type
func numberOfSections(in tableView: UITableView) -> Int {
return numberOfSections
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
}
private extension {
// this would contain everything that shoudn't be visible for other extensios
var secret: Int = 0
public extension MyFriendClass {
// oh, well, I make an exception here for a trustworthy type
func checkSecret(of controller: MyVC) -> Bool {
return controller.secret > 0
}
}
private extension {
// this is so secret, I'm not even allowed to copy it
}
}
public func myMethod() {
print("This is just a boring method")
}
}
It has the downside of shifting code to the right (you could as well leave those extension-blocks unindented), but lots of advantages:
- No change for private needed
- It can be nested as much as you like to satisfy even the most absurd desires of encapsulation
- It reminds me on operator definitions inside type declarations
- No change for fileprivate needed (but actually, I think there is very little need to keep fileprivate)
I wish this would only be a joke, but writing the example, I actually started liking the concept (but I have a terrible headache right now which might affect my mind) — so either this receives some feedback, or I might start re-proposing this ;-)
- Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170408/5cc65e97/attachment.html>
More information about the swift-evolution
mailing list