Protocol inheritance


My notes on Protocol Inheritance

insert class hierarchy diagram here.

Rational

Say you have 3 classes that needs to call the same method with the same code. But you cant extend a new BaseClass. Because the BaseClass is different in each 3 classes. You need to override the same method in all classes “scroll” And you want to be able to call the “super” There is no super with pop (protocol oriented programming)

/**
 * This setup describes how classes may call methods and the methods call their "super" method. 
 * Inheritance with protocols. retaining the ability to call "super" and the ability to override
 */
protocol Scrollable{
    func scroll()
}
extension Scrollable{
    func scroll(){
        defaulScroll()
    }
    func defaulScroll(){
        Swift.print("a")
    }
}
class A:Scrollable{}
protocol BScrollable:Scrollable{}
extension BScrollable{
    func scroll() {
        bScroll()
    }
    func bScroll(){
        Swift.print("b")
        defaulScroll()
    }
}
class B:BScrollable{}
//how do we make c call b call a
protocol CScrollable:BScrollable{}
extension CScrollable{
    func scroll() {
        cScroll()
    }
    func cScroll(){
        Swift.print("c")
        bScroll()
    }
}
class C:CScrollable{}

let a = A()
a.scroll()//a
print("")
let b = B()
b.scroll()//b, a
print("")
let c = C()
c.scroll()//c , b , a

An alternative is to :

protocol X{}
protocol Y{}
protocol Z{}
extension X{func render(print("x")){}}
extension Y{func render(print("y")){}}
extension Z{func render(print("z")){}}
class A:X,Y,Z{
    func render(){//can be called from a Super class
        (self as X).render()
        (self as Y).render()
        (self as Z).render()
    }
}
let a = A()
a.render()//z,x,y

Related Posts

Infinite Tree List

My notes on Infinite tree list

Protocol Ambiguity

How to differentiate protocol ambiguity

The Ultimate Xcode Workflow

Spend zero time managing dependencies

Faster Xcode With Spm

How you can speed up compile times in XCode with Swift Package Manager

Spm And Ci Travis

My notes on Swift PM + CI Travis

Spm And Nested Frameworks

My notes on Swift package manager + XCode + Nested frameworks

Xcode And Spm

Here is how you use Swift package manager in your XCode app projects

Carthage And Nested Frameworks

A few workflows concerning Carthage and nested framework

Two Finger Swipe

Notes on implementing two finger swipe for macOS

Switch

Notes about the Switch component