concept default implementation in category swift

appears as: default implementations, default implementation, default implementation, default implementations
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

Swift’s protocol extensions are another of its major selling points and are one of the most powerful features it has to offer. Protocols can act like interfaces; protocol extensions offer default implementations to types, helping you avoid rigid subclassing trees.

Protocols, however, are trickier than they may seem and may surprise even the seasoned developer. For instance, let’s say you have a protocol called FlavorType representing a food or drink item that you can improve with flavor, such as coffee. If you extend this protocol with a default implementation that is not found inside the protocol declaration, you may get surprising results! Notice in the next listing how you have two Coffee types, yet they both yield different results when calling addFlavor on them. It’s a subtle but significant detail.

Listing 1.2. Protocols can surprise us
protocol FlavorType{
//    func addFlavor() // You get different results if this method doesn't exist.
}

extension FlavorType {
    func addFlavor() { // Create a default implementation.
        print("Adding salt!")
    }
}
struct Coffee: FlavorType {
    func addFlavor() { // Coffee supplies its own implementation.
        print("Adding cocoa powder")
    }
}


let tastyCoffee: Coffee = Coffee() // tastyCoffee is of type 'Coffee'
tastyCoffee.addFlavor() // Adding cocoa powder

let grossCoffee: FlavorType = tastyCoffee // grossCoffee is of type FlavorType
grossCoffee.addFlavor() // Adding salt!

Besides declaring a method signature, with protocols you can supply full implementations. Extending a protocol means that you can offer default implementations to a protocol so that types don’t have to implement certain methods. The benefits are profound. You can elegantly bypass rigid subclassing structures and end up with highly reusable, flexible code in your applications.

In its purest form, a protocol extension sounds simple. Offer a default implementation and be on your merry way. Moreover, protocol extensions can be easy to grasp if you stay at the surface. But as you progress through this chapter, you’ll discover many different use cases, pitfalls, best practices, and tricks related to correctly extending protocols.

Listing 12.2. Implementing a protocol with default implementation
struct BikeRequestBuilder: RequestBuilder {                               #1
     let baseURL: URL = URL(string: "https://www.biketriptracker.com")!   #1
 }

let bikeRequestBuilder = BikeRequestBuilder()
let request = bikeRequestBuilder.makeRequest(path: "/trips/all")          #2
print(request) // https://www.biketriptracker.com/trips/all               #3

12.3.1. Overriding a default implementation

To see how protocol inheritance works, imagine having a protocol Tree with a method called grow(). This protocol offers a default implementation via a protocol extension. Meanwhile, an Oak struct implements Tree and also implements grow() (see figure 12.6).