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

Rudolf Adamkovic salutis at me.com
Sat Feb 27 14:38:54 CST 2016


+1

I'd love to see this in the language. 

Hope somebody from the core team will comment on why this is not allowed.

R+

Sent from my iPhone

> On 27 Feb 2016, at 11:31, 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
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160227/d35c1aaa/attachment.html>


More information about the swift-evolution mailing list