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-117] Calling default implementation of protocols #42739

Open
swift-ci opened this issue Dec 7, 2015 · 5 comments
Open

[SR-117] Calling default implementation of protocols #42739

swift-ci opened this issue Dec 7, 2015 · 5 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 7, 2015

Previous ID SR-117
Radar None
Original Reporter cojoj (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: 222f3e1dbdbb54005302e8c206e80dfb

relates to:

  • SR-1842 Introduce syntax for explicitly invoking extension methods.
  • SR-9792 Provide some way to call a protocol's base's overload of a method.

Issue Description:

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 nor 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")
    }
}
@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2015

As a language change, this should go through the Swift Evolution Process.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 8, 2015

Comment by Mateusz Zając (JIRA)

@belkadan would you rather see it as a PR to https://github.com/apple/swift-evolution repo?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 8, 2015

Comment by Radek Pietruszewski (JIRA)

cojoj (JIRA User) You should start a discussion on the Evolution mailing list first before submitting a PR

@swift-ci
Copy link
Collaborator Author

Comment by Brian (JIRA)

You can do `(self as Foo).testPrint()` to invoke the default method. Not the most elegant or discoverable syntax, but.

@typesanitizer
Copy link

Related discussion for enums: https://forums.swift.org/t/super-description-for-enums/49174

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

3 participants