files image

Predicament test – what’s the output of this code?

protocol Greeter {
    func greet()
}

extension Greeter {
    func greet() {
        print("Hiya, World!")
    }
}

class BaseGreeter: Greeter {}

class LazyGreeter: BaseGreeter {
    func greet() {
        print("sup")
    }
}

let greeter: Greeter = LazyGreeter()
greeter.greet()

Even seasoned Swift developers can even secure this infamous, so don’t feel despicable in case your solution became as soon as “sup”. The actual output is “Hiya, World!”, certain surely… scuttle forward and lunge it in a Playground.

So what’s happening? It’s all all the type down to how Swift handles dispatch of protocol sorts. A Protocol Peek Table1 (PWT) is the mechanism Swift makes use of to protect song of sorts that put into effect a protocol. At the same time as you name a feature, or secure correct of entry to a property on a protocol style, the PWT is frail to name the concrete implementation to name. The gotcha is that PWTs are simplest linked to sorts that say their conformance to a protocol. Notify that I acknowledged say, and no longer simply conform. In this instance, simplest BaseGreeter explicitly proclaims conformance to Greeter, thus the PWT is simplest linked to BaseGreeter. PWTs plan no longer tumble to subclasses, as subclasses plan no longer explicitly say their conformance. Therefore, for the explanation that greeter native let is of fashion Greeterno longer LazyGreeter – the resolution to greet() defaults to the implementation in the Greeter extension, as there is no PWT linked to LazyGreeter.

To account for, LazyGreeter().greet() does output “sup” as you’d seek files from. The anguish is particularly the use of the Greeter protocol style for the native let that introduces indirection and the need for the PWT. In say for this instance to output the anticipated “sup”, we’d must put into effect greet() in BaseGreeter so that LazyGreeter can override it. That’s no longer perfect, and brings into request of your explanation for using a protocol extension in the first reputation.

Read More:  How Michigan (not Michigan State, Purdue or Ohio State) became the Big Ten team in the Final Four

Would you peep this malicious program at some stage in code overview? I can’t confidently advise that I’d. Even extra caring, is that whereas you beget been to write a unit test for LazyGreeter, you’d likely lawful use let greeter = LazyGreeter(), thus bypassing Greeter and the PWT altogether. Each and each these factors compose this an extremely unsuitable gotcha, so aid your eyes out – it is probably going you’ll presumably effectively save yourself or a teammate from somewhat a number of head scratching!

Fortunately Periphery has your aid, and it’ll correctly file that LazyGreeter.greet() is unused. Granted, you are going to be just a miniature puzzled on the origin and style if Periphery has made a mistake. Right here’s a enormous instance of why my unofficial slogan for Periphery is: “So appropriate, you’ll focal level on it’s broken”.

1: WWDC 2016: Figuring out Swift Efficiency – PWT explanation begins at 24:30.

Read More