Calling default implementation of protocols in Swift

Originator:matthewhare91
Number:rdar://22720226 Date Originated:16-Sep-2015 05:47 PM
Status:Closed Resolved:
Product: Product Version:
Classification: Reproducible:
 
Summary:
If you define a simple protocol like:

protocol Foo {
    func testPrint()
}

And than you provide a default implementation for testPrint() method in protocol extensions like:

extension Foo {
    func testPrint() {
        print("Protocol extension call")
    }
}

You aren't allowed to call the fault implementation from the structure eg.

struct Bar: Foo {
    func testPrint() {
        self.testPrint()
        print("Call from struct")
    }
}

This is some sort of limitation as often happens that a default implementation is providing a major part of implementation and only one, simple line is changed in actual struct implementation. If you're using classes you can achieve this by creating a base class and calling a method on super. If you consider structs, there's no such possibility and you always have to write a whole implementation from scratch in each structure which conforms to the protocol.

---
You can use composition by creating nested structure, but it's neither logical or clean... It's rather a hack...

struct Bar: Foo {
    func testPrint() {
        // Calling default implementation
        struct Dummy : Foo {}
        let dummy = Dummy()
        dummy.testPrint()
        print("Call from struct")
    }
}

Steps to Reproduce:


Expected Results:
Ability to call default implementation of protocol methods from conforming type.

Actual Results:
Won't compile...?

Version:
Xcode 7.0 GM + Swift 2.0

Notes:
http://stackoverflow.com/questions/32602712/calling-protocol-default-implementation-from-regular-method

https://twitter.com/jckarter/status/644154804218953731

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!