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-3616] Default implementation being called when specialized one should be picked #46201

Closed
swift-ci opened this issue Jan 12, 2017 · 2 comments
Closed
Labels

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jan 12, 2017

Previous ID SR-3616
Radar None
Original Reporter TadeasKriz (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

OS X El Capitan 10.11.6
Xcode 8.2 (8C38)
Swift 3 - default Xcode 8.2 toolchain

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

md5: 37d41da5c66e66345ea5bd0549666027

duplicates:

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

Issue Description:

The following code has unexpected results.

protocol Component: class {
    func test() -> Int
}

extension Component {
    func test() -> Int {
        return -1
    }
}

class BaseComponent: Component {
    let delegate = ComponentDelegate<BaseComponent>()

    init() {
        delegate.component = self
    }
}

class ChildComponent: BaseComponent {
    func test() -> Int {
        return 10
    }
}

final class ComponentDelegate<COMPONENT: Component> {
    weak var component: COMPONENT?

    func runTest() -> Int? {
        return component?.test()
    }
}

let x = BaseComponent()
let y = ChildComponent()

x.test() // Returns -1
y.test() // Returns 10

x.delegate.runTest() // Returns -1
y.delegate.runTest() // Returns -1

Results from the delegated functions seem to be wrong and the default implementation is being used. A way to workaround this is to implement the `test` method in the `BaseComponent` class. Then all child classes work as well.

I tried changing the `delegate` property type to `ComponentDelegate<ChildComponent>` and then forcefully casting `self` in the init, but this produces the same results = calling the default implementation.

Is this a bug in Swift or intended behavior? If it's an intended behavior, it seems a bit confusing to me and might be unnecessary constraining.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jan 12, 2017

Known issue!

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Jan 12, 2017

Comment by Tadeas Kriz (JIRA)

Oh, thanks, I didn't find that issue.

@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
Projects
None yet
Development

No branches or pull requests

2 participants