You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
protocolMyProtocol: AnyObject { }
classMyClass { }
classMySubclass: MyClass, MyProtocol { }
classMyGenericClass<T> {
funcfoo(witho: T) { }
}
extensionMyGenericClasswhereT: MyClass {
funcbar(witho: 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 fromextensionMyGenericClasswhereT: MyClass & MyProtocol {
funcbaz(witho: T) { }
}
// works fine; 'MySubclass' fills the requirement for 'MyClass & MyProtocol'// in this contextvarmyClass: (MyClass & MyProtocol)?
myClass = MySubclass()
leti = MyGenericClass<MyClass>()
i.foo(with: MySubclass())
i.bar(with: MySubclass())
letj = MyGenericClass<MyClass & MyProtocol>()
j.foo(with: MySubclass())
j.bar(with: MySubclass()) // errorj.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!
The text was updated successfully, but these errors were encountered:
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.
Additional Detail from JIRA
md5: 199e398a3105e390dde3f33e16a96d64
duplicates:
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:If I try to add a constraint on
MyGenericClass.T
that it must be anAnyObject
, I get this error when I declarej
:''MyGenericClass' requires that 'MyClass & MyProtocol' be a class type"
. I expected thatMySubclass
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!
The text was updated successfully, but these errors were encountered: