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-4625] Compiler reports an error for protocol conformance for method with default parameter value #47202

Open
swift-ci opened this issue Apr 19, 2017 · 3 comments
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances default arguments Feature: default arguments for value parameters feature A feature request or implementation protocol Feature → type declarations: Protocol declarations swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-4625
Radar rdar://problem/18547983
Original Reporter AFilimonov (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, TypeChecker
Assignee None
Priority Medium

md5: 9f3652ed2c3781923f5eef96e067f81b

Issue Description:

For example in a code like this:

protocol SomeProtocol {
    func someFunction()
    func someFunction(parameter: String)
}

class SomeClass: SomeProtocol {
    func someFunction(parameter: String = "") {
        if parameter.characters.count > 0 {
            print("string: " + parameter)
        } else {
            print("empty string")
        }
    }
}

Compiler reports an error, that there's no implementation for func someFunction() in SomeClass. For example I got such report in playground:

Playground execution failed: error: MyPlayground.playground:38:7: error: type 'SomeClass' does not conform to protocol 'SomeProtocol'
class SomeClass: SomeProtocol {
      ^

MyPlayground.playground:34:10: note: protocol requires function 'someFunction()' with type '() -> ()'; do you want to add a stub?
    func someFunction()
         ^

MyPlayground.playground:39:10: note: candidate has non-matching type '(String) -> ()'
    func someFunction(parameter: String = "") {
         ^

but it's possible to execute following code:

let item = SomeClass()
item.someFunction()

So I believe this class could be treated as conforming to the SomeProtocol protocol.
The workaround is easy but requires boilerplate code. It would be great if compiler would take default argument values into account while checking protocol conformance.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@vanvoorden
Copy link
Contributor

I think this has been a problem this whole time. Were there ever any discussions about supporting this?

@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal conformances Feature → protocol: protocol conformances protocol Feature → type declarations: Protocol declarations default arguments Feature: default arguments for value parameters and removed type checker Area → compiler: Semantic analysis improvement labels Apr 16, 2024
@AnthonyLatsis
Copy link
Collaborator

Likely to have been on the forums. The workaround is this, FWIF:

protocol P {
    func f()
    func f(s: String)
}

class C: P {
  func f() {
    self.f(s:)()
  }
  func f(s: String = "") {
    // implementation
  }
}

@vanvoorden
Copy link
Contributor

@AnthonyLatsis Ahh… interesting! Thanks!

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 conformances Feature → protocol: protocol conformances default arguments Feature: default arguments for value parameters feature A feature request or implementation protocol Feature → type declarations: Protocol declarations swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

3 participants