Search code examples
swift3swift-protocolsswift-extensions

How is swift allowing same method in protocol and protocol extension?


Here is the map method which seems to be duplicate in Sequence protocol. How is apple doing this? what is the intention behind this?

public protocol Sequence {
public func map<T>(_ transform: (Self.Iterator.Element) throws -> T) rethrows -> [T]
}

extension Sequence {
public func map<T>(_ transform: (Self.Iterator.Element) throws -> T)   rethrows -> [T]
}

Solution

  • The protocol Sequence both requires a map function and (using a protocol extension) supplies a default implementation for it. That is what protocol extensions are for.