Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling protocol default implementation from regular method

I'm wondering if it's possible to achieve such a thing.
I have a Playground like this:

protocol Foo {     func testPrint() }  extension Foo {     func testPrint() {         print("Protocol extension call")     } }  struct Bar: Foo {     func testPrint() {         // Calling self or super go call default implementation         self.testPrint()         print("Call from struct")     } }   let sth = Bar() sth.testPrint() 

I can provide a default implementation in extension but what if Bar needs everything that is in default implementation plus additional things?
It's somehow similar to calling super. methods in classes to fulfill requirement of implementing every property etc. but I see no possibility to achieve the same with structs.

like image 463
cojoj Avatar asked Sep 16 '15 07:09

cojoj


People also ask

What is protocol and how do you implement it?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

What is default protocol in Swift?

Protocol Default Implementation “You can use protocol extensions to provide a default implementation to any method or computed property requirement of that protocol.” In fact, not only can you provide a default implementation to methods or computed properties defined in the protocol, you can also add new ones.

How many protocols can a Swift class adopt?

Swift 4 allows multiple protocols to be called at once with the help of protocol composition.

How do I add a protocol in Swift?

To create a protocol, use the protocol keyword followed by the name you want and defined by the curly braces. Protocols can be of 2 types: read-only/read-write. Read-only means you can only get the variable, but you cannot set it. Read-write means you can both set and get properties.


1 Answers

I don't know if you are still looking for an answer to this, but the way to do it is to remove the function from the protocol definition, cast your object to Foo and then call the method on it:

protocol Foo {      // func testPrint() <- comment this out or remove it }  extension Foo {     func testPrint() {         print("Protocol extension call")     } }  struct Bar: Foo {     func testPrint() {         print("Call from struct")         (self as Foo).testPrint() // <- cast to Foo and you'll get the  default                                   //    function defined in the extension     } }  Bar().testPrint()  // Output:    "Call from struct" //            "Protocol extension call" 

For some reason it only works if the function isn't declared as part of the protocol, but is defined in an extension to the protocol. Go figure. But it does work.

like image 196
Aaron Rasmussen Avatar answered Sep 22 '22 20:09

Aaron Rasmussen