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

Rasmus Kjeldsen rasmus.kjeldsen at gmail.com
Sat Feb 27 04:31:34 CST 2016


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


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


More information about the swift-evolution mailing list