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-7129] Inconsistent dispatch with protocol extensions #49677

Closed
swift-ci opened this issue Mar 6, 2018 · 1 comment
Closed

[SR-7129] Inconsistent dispatch with protocol extensions #49677

swift-ci opened this issue Mar 6, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 6, 2018

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

md5: 6525164d8137f24816d939df306a46ac

duplicates:

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

Issue Description:

Not sure if this intentional or a bug, but it make the use of protocol extensions seem dangerous IMO. Run the following playground code:

public protocol Testable {
    func test1()
    func test2()
}

public extension Testable {
    func test1() { print("FAIL 1a") }
    func test2() { print("FAIL 2") }
}

class TestBase: Testable {
    func test1() { print("FAIL 1b") }
}

class TestChild: TestBase {
    override func test1() { print("PASS 1") }
    func test2() { print("PASS 2") }
}

let testChild: Testable = TestChild()
testChild.test1()
testChild.test2()

Expected output:

PASS 1
PASS 2

Actual output:

PASS 1
FAIL 2 

However, with the parent class eliminated:

class TestChild: Testable {
    func test1() { print("PASS 1") }
    func test2() { print("PASS 2") }
}

the output is as expected:

PASS 1
PASS 2

So one must be conscious of the ancestry of the class in order to know if a method conforming to a protocol can be invoked or not.

@belkadan
Copy link
Contributor

belkadan commented Mar 6, 2018

The underlying issue is SR-103, but note that the same is still true with protocol extensions that don't match requirements in the protocol. Overload resolution is done at compile time. Sometimes this is useful. (For instance, if the methods were implemented in completely separate libraries, they wouldn't interfere with one another.)

@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

2 participants