[swift-evolution] Idea: Named extensions

Matthew Johnson matthew at anandabits.com
Mon May 16 13:58:11 CDT 2016



Sent from my iPad

> On May 16, 2016, at 12:33 PM, Michael Peternell via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Why not just use a (documentation) comment?
> 
> /// The Lifecycle extension:
> extension ViewController {

There have been discussions about possibly allowing extensions to have stored properties in the future.  If that comes to pass it may well be useful for extensions to have a name that can be used to refer to the extension during initialization.  I discussed possible syntax for that in my draft proposal for partial initializers (which I tabled for Swift 3, but may revive in the future).


> ...
> 
> -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
> 
> _______________________________________________
> 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