Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1271] Protocol extension method being called over subclass implementation. #43879

Closed
swift-ci opened this issue Apr 20, 2016 · 1 comment
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1271
Radar None
Original Reporter dlbuckley (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 7.3, Swift 2.2, OSX 10.11.4

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 606381dd1b70783cd53f6d0bee513f00

duplicates:

  • SR-103 Protocol Extension: function's implementation cannot be overridden by a subclass
  • SR-118 Protocol extensions cannot be overridden in subclasses

Issue Description:

I have come across an issue where I want to have a class conform to a protocol by default, but the protocol has default implementations of all it's methods through an extension. I then want to subclass the original class and optionally implement the protocol methods.

Unfortunately by trying to do this I ran into a problem where the methods in the protocol extension were being called over the methods implemented in the subclass which I believe is unintended behaviour. The methods implemented in the main class were being called instead of the protocol extension methods fine.

I have included a Xcode playground to demonstrate this behaviour.

protocol MyProtocol {
    func doTheFirstThing()
    func doTheSecondThing()
    func doTheThirdThing()
}

extension MyProtocol {
    func doTheFirstThing() {
        print("The DEFAULT first thing")
    }
    
    func doTheSecondThing() {
        print("The DEFAULT second thing")
    }
    
    func doTheThirdThing() {
        print("The DEFAULT third thing")
    }
}

class MyClass: MyProtocol {
    func myMethod() {
        self.doTheFirstThing()
        self.doTheSecondThing()
        self.doTheThirdThing()
    }
    
    func doTheFirstThing() {
        print("This implementation of the first thing should be called instead of the default implementation")
    }
}

class MySubclass: MyClass {
    
    func doTheSecondThing() {
        print("This implementation of the second thing should be called instead of the default implementation")
    }
}

MySubclass().myMethod()
// This implementation of the first thing should be called instead of the default implementation
// The DEFAULT second thing (This should be the implementation from the subclass)
// The DEFAULT third thing
@swift-ci
Copy link
Collaborator Author

Comment by Dale Buckley (JIRA)

After having a search around I've found this is a duplicate of SR-118, but according to that ticket it was resolved on 8th December 2015. As it stands that was several months before Swift 2.2 so I would assume that it would be included in that release, but this is not the case.

I don't know where to track this particular fix in the codebase as there are no references to releases in the resolved issue. I will link this open issue to it as a duplicate, but it's an ongoing issue that has yet to be resolved.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant