[swift-evolution] Idea: Named extensions

Brandon Knope bknope at me.com
Mon May 16 11:26:38 CDT 2016


I like to separate methods into their own logical extensions so similar methods are grouped together. I do this mostly with Cocoa Touch where I like all view life cycle methods to be in the same extension:

extension ViewController {
    override func viewDidLoad() {
    }
    
    override func viewWillAppear(animated: Bool) {
    }
    
    override func viewDidDisappear(animated: Bool) {
    }
}

You can document this somewhat by adding a MARK comment:

// MARK: Lifecylce
extension ViewController {
    override func viewDidLoad() {
    }
    
    override func viewWillAppear(animated: Bool) {
    }
    
    override func viewDidDisappear(animated: Bool) {
    }
}

What if we made this more self-documenting by elevating this to a language feature?

extension ViewController named Lifecycle {
    override func viewDidLoad() {
    }
    
    override func viewWillAppear(animated: Bool) {
    }
    
    override func viewDidDisappear(animated: Bool) {
    }
}

Other ways:
extension named Lifecycle ViewController { }
extension named “View Lifecycle" ViewController { }
extension ViewController named “Multi word description” { }


For now, this is purely a documenting feature (i.e. Can’t refer to the extension name dynamically or statically in actual code). I think it plays much more naturally with Swift than requiring this to be in the comments and would work across all IDEs and make it easier for people to find a specific extension as well as making their code more self documenting.

Any thoughts?

Thanks,
Brandon



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160516/c5be0ee6/attachment.html>


More information about the swift-evolution mailing list