[swift-evolution] [Proposal] Allow static protocol extension methods to be called directly on the protocol

Maximilian Hünenberger m.huenenberger at me.com
Wed Mar 23 09:13:09 CDT 2016


Wouldn't it be easier (syntax wise) to extend the meta type directly? :

protocol P {}
extension P.Type {
        static func a() {}
}

Kind regards
- Maximilian

> Am 28.02.2016 um 02:05 schrieb Joe Groff via swift-evolution <swift-evolution at swift.org>:
> 
> Rasmus Kjeldsen via swift-evolution
> <swift-evolution at swift.org> wrote:
>> 
>> I propose to allow static protocol extension methods to be used directly on the protocol.
>> This would enable the Factory-pattern to be elegantly implemented as in the example below:
>> 
>> protocol P {}
>> 
>> class A : P {}
>> class B : P {}
>> 
>> extension P {
>>    static func factory(type: Bool) -> P { return type ? A() : B()}
>> }
>> 
>> let a = P.factory(false) // <- Not possible in Swift 2, factory() can
>> only be used on either A or B
>> 
>> Regards
>> 
>> Rasmus Friis Kjeldsen
> 
> 'extension P' extends all types that conform to P. We could introduce a new
> syntax for extending the protocol type, maybe:
> 
> extension protocol<P> { ... }
> 
> This might be an important part of fleshing out our support for protocol
> existential types. It would offer a way for protocol types to conform to
> their own protocols even when Self or associated types make that normally
> impossible. 
> 
> -Joe 
> 
> 
> _______________________________________________
> 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