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-6681] A subclass of a base class which conforms to a protocol with a requirement fulfilled by a default implementation cannot override the base class' method #49230

Closed
swift-ci opened this issue Dec 31, 2017 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6681
Radar None
Original Reporter neightchan (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 5dc9fbcf9063633b4a5652697967d80e

duplicates:

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

Issue Description:

The following code compiles:

protocol P {
    func foo()
}

extension P {
    func foo() {
        print("P.foo()")
    }
}

class B : P {}

class D : B {
    func foo() {
        super.foo()
        print("D.foo()")
    }
}

Note in particular that foo() isn't decorated with override, but appears to be overriding the function. (Aside: in fact D's foo() is not an override of B's foo() which can be observed by casting D to be and calling foo(): (D() as B).foo() prints P.foo().)

Ideally, the behavior of

protocol P {
    func foo()
}
extension P {
    func foo() {
        print("P.foo()")
    }   
}

class B : P {
}

and

protocol P {
    func foo()
}

class B : P {
    func foo() {
        print("B.foo()")
    }   
}

would behave "the same" in the context of subclassing: In either case, the first code snippet would not compile (since the override keyword is missing), and instead the following would (which currently does not):

protocol P {
    func foo()
}

extension P {
    func foo() {
        print("P.foo()")
    }
}

class B : P {
}

class D : B {
    override func foo() {
        super.foo()
        print("D.foo()")
    }
}

and writing (D() as P).foo() would print

P.foo()
D.foo()
@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