[swift-evolution] Idea: Named extensions

Michael Peternell michael.peternell at gmx.at
Mon May 16 12:33:32 CDT 2016


Why not just use a (documentation) comment?

/// The Lifecycle extension:
extension ViewController {
...

-Michael

> Am 16.05.2016 um 18:26 schrieb Brandon Knope via swift-evolution <swift-evolution at swift.org>:
> 
> 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
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list