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:
When trying to receive a sub-protocol from a generic method constrained by a parent protocol, the compiler fails, returning an error saying that the "Generic parameter 'T' could not be inferred":
protocolParent{vartitle:String{get}}protocolChild:Parent{varsubtitle:String{get}}func genericConstraint<T:Parent>(options:[String:Any], completion:(T)->Void){//Create concrete implementation based on options passed}//The compiler fails to accept 'Child' as type with the error message "Generic parameter 'T' could not be inferred"genericConstraint(options:[:]){(child:Child)in//The compiler mistakenly throws the error "Ambiguous reference to member 'print(_:separator:terminator)'"print(response.subtitle)}
This constraints could be useful when having multiple types (like structs Child1, and Child2) that could be create based on different options, also, when "hidding" types under protocol, like abstracting CoreData implementation from other app layers.
The text was updated successfully, but these errors were encountered:
It's a bad error message, but it's correct that this doesn't compile: the type Child doesn't itself conform to the protocol Parent. You have to use a concrete type here.
(I don't know if the language has a way to spell what you're trying to do, "any protocol value where the protocol inherits from the original type".)
cc @huonw for the feature and @xedin for the diagnostic
The diagnostic for this issue has been improved in Xcode 13 (changing `response` to `child.subtitle` because it doesn't exist):
error: protocol 'Child' as a type cannot conform to 'Parent'
genericConstraint(options: [:]) { (child:Child) in
^
note: only concrete types such as structs, enums and classes can conform to protocols
genericConstraint(options: [:]) { (child:Child) in
^
note: required by global function 'genericConstraint(options:completion:)' where 'T' = 'Child'
func genericConstraint<T: Parent>(options: [String: Any], completion: (T)->Void) {
^
Please use either Xcode 13 or main branch snapshot to verify and close this issue.
Environment
Xcode Version 9.3 (9E145)
Additional Detail from JIRA
md5: 84b2a530941991d3fb156f636ab75f11
relates to:
Issue Description:
When trying to receive a sub-protocol from a generic method constrained by a parent protocol, the compiler fails, returning an error saying that the "Generic parameter 'T' could not be inferred":
This constraints could be useful when having multiple types (like structs Child1, and Child2) that could be create based on different options, also, when "hidding" types under protocol, like abstracting CoreData implementation from other app layers.
The text was updated successfully, but these errors were encountered: