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-8637] 'Protocol composition type' with generic types/inheritance #51152

Closed
swift-ci opened this issue Aug 24, 2018 · 1 comment
Closed

[SR-8637] 'Protocol composition type' with generic types/inheritance #51152

swift-ci opened this issue Aug 24, 2018 · 1 comment
Labels

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Aug 24, 2018

Previous ID SR-8637
Radar None
Original Reporter Aaron Bosnjak (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 199e398a3105e390dde3f33e16a96d64

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

I've been running into some problems with trying to use a 'protocol composition type' (e.g. MyType & MyProtocol, I'll refer to it as a PCT for brevity) with generics, both by trying to use it as a generic constraint and as the parameter to a generic type. This code sample more succinctly shows my problem in less technical words:

protocol MyProtocol: AnyObject { }
class MyClass { }
class MySubclass: MyClass, MyProtocol { }
class MyGenericClass<T> {
    func foo(with o: T) { }
}
extension MyGenericClass where T: MyClass {
    func bar(with o: T) { }
}
// this extension compiles. My understanding, though, is that it's treating the
// PCT as a protocol to conform to rather than a class to inherit from
extension MyGenericClass where T: MyClass & MyProtocol {
    func baz(with o: T) { }
}

// works fine; 'MySubclass' fills the requirement for 'MyClass & MyProtocol'
// in this context
var myClass: (MyClass & MyProtocol)?
myClass = MySubclass()

let i = MyGenericClass<MyClass>()
i.foo(with: MySubclass())
i.bar(with: MySubclass())

let j = MyGenericClass<MyClass & MyProtocol>()
j.foo(with: MySubclass())
j.bar(with: MySubclass()) // error
j.baz(with: MySubclass()) // error

If I try to add a constraint on MyGenericClass.T that it must be an AnyObject, I get this error when I declare j: ''MyGenericClass' requires that 'MyClass & MyProtocol' be a class type". I expected that MySubclass would fill the type requirements on the last two lines, but it doesn't - is this intended, or just an overlooked detail due to the way PCTs are implemented?

Thanks!

@belkadan
Copy link
Contributor

@belkadan belkadan commented Aug 30, 2018

It's correct behavior; the representation of a composition type is different than the representation of "a type that has these two constraints". You could use == in your constraint instead of :, but I suspect that's not what you want either.

@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