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-4694] Subtyping Properties and Return Types for Protocol Conformance #47271

Closed
swift-ci opened this issue Apr 25, 2017 · 0 comments
Closed
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances duplicate Resolution: Duplicates another issue feature A feature request or implementation improvement protocol Feature → type declarations: Protocol declarations type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 25, 2017

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

md5: d8be69378d06edc0fb0afb6caecc6493

duplicates:

  • SR-522 Protocol funcs cannot have covariant returns

Issue Description:

Subtyping a property or a return type in a Protocol conforming type is not possible in Swift 3.1

Example:

protocol MyType {
}

class Stub : MyType {
}

protocol HasMyType {
    var a: MyType { get }

    func getA() -> MyType
}

struct TestA: HasMyType {
    var a: MyType

    func getA() -> MyType {
        return Stub()
    }
}

struct TestB: HasMyType {
    var a: Stub

    func getA() -> Stub {
        return Stub()
    }
}

Since Stub conforms to MyType it is possible to set property a in TestA to a Stub (or return a Stub in getA). So far so good. But it is not possible to specify the property or the return type in a HasMyType-conforming Type to a concrete subtype of MyType.

For Example you setup a testcase and use a Stub as your implementation of MyType and want to refer to functions and properties only visible in Stub.

Compiler Output:

Playground execution failed: error: SwiftPlayground.playground:19:8: error: type 'TestB' does not conform to protocol 'HasMyType'
struct TestB: HasMyType {
       ^

SwiftPlayground.playground:6:9: note: protocol requires property 'a' with type 'MyType'; do you want to add a stub?
    var a: MyType { get }
        ^

SwiftPlayground.playground:20:9: note: candidate has non-matching type 'Stub'
    var a: Stub
        ^

SwiftPlayground.playground:8:10: note: protocol requires function 'getA()' with type '() -> MyType'; do you want to add a stub?
    func getA() -> MyType
         ^

SwiftPlayground.playground:22:10: note: candidate has non-matching type '() -> Stub'
    func getA() -> Stub {
         ^
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself feature A feature request or implementation conformances Feature → protocol: protocol conformances protocol Feature → type declarations: Protocol declarations duplicate Resolution: Duplicates another issue type checker Area → compiler: Semantic analysis improvement and removed new feature labels Jan 27, 2023
This issue was closed.
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 duplicate Resolution: Duplicates another issue feature A feature request or implementation improvement protocol Feature → type declarations: Protocol declarations type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants