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-6872] Protocol as a generic argument is not suitable for conformance checking #49421
Comments
@jckarter, do we have something tracking this already? |
I can't find it, but this is a known design issue. The code is behaving correctly as currently designed—`U.Type` in the generic context represents a concrete metatype, so when you substitute `U = P` you get the concrete metatype for P, `P.Protocol`. This ends up asking "is S the type P", which is false, rather than "does S conform to P". A slightly different formulation works: protocol P {}
struct S: P {}
func does<T, U>(_ x: T, extend _: U.Type) -> Bool {
print(U.self) // P.Type
return x is U
}
print(P.Type.self) // P.Type
S.self is P.Type // true
does(S.self, extend: P.Type.self) // false |
Whatever you meant But it's good to know you are aware of this limitation. |
Sorry, I updated the comment. The new version should print `true`. You have to provide `Protocol.Type` as a generic argument in whole in order to get the behavior you want. |
@jckarter This time it really does what was expected. Thank a lot for this workaround! |
Joe, what do you want to use this bug for? Diagnostics? Changing the interpretation? |
It feels to me like a language design issue we should revisit at some point. |
Environment
Xcode 9.2 (9C40b), Swift 4.0.3
Additional Detail from JIRA
md5: 5285a477e2b79af708f9d2261a82a502
Issue Description:
It is possible to check if a generic parameter
T
is a subtype of generic parameterU
if both are classes, but ifU
is a protocol, the check is impossible.Xcode 9.2 (9C40b)
The text was updated successfully, but these errors were encountered: