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-13296] Associated type inference through constraints on sibling associated type #55736

Open
jeremyabannister opened this issue Jul 25, 2020 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@jeremyabannister
Copy link

Previous ID SR-13296
Radar None
Original Reporter @jeremyabannister
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 73808fb6010e083ce8ab513b19743c6a

Issue Description:

      • UPDATE:

After thinking about this and experimenting with it a bit more I realized that the real thing I'm requesting is even simpler to demonstrate:

protocol A {
    associatedtype Foo
}


protocol B: A {
    var foo: Foo { get }
}


struct C: B {
    
    var foo: Int {
        0
    }
}

Shouldn't the compiler be able to infer the type of Foo based on the implementation of C?

— ORIGINAL POST:

Imagine I have a protocol A:

protocol A {
    
    associatedtype B
    
    associatedtype C: Collection where C.Element == B
    
    var c: C { get }
}

I was hoping to be able to conform to it like this:

struct X: A {
    
    var c: [Int] {
        [1, 2, 3]
    }
}

Is this manner of type inference supposed to work?

I understand that another approach in this case would be to remove B as an associated type and make it a typealias:

protocol A {
    
    associatedtype C: Collection
    
    var c: C { get }
}


extension A {
    typealias B = C.Element
}


struct X: A {
    
    var c: [Int] {
        [1, 2, 3]
    }
}

but this doesn't help me because in my real-world example the "sibling" associated type ("B") is actually inherited from the parent protocol, and must be satisfied. Here's a different version of my same request. I would like for this code to compile:

protocol A {
    associatedtype X
}

protocol B: A {

    associatedtype C: Collection where C.Element == X
    
    var c: C { get }
}

struct Y: B {

    var c: [Int] {
        [1, 2, 3]
    }
}
@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant